| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Package format implements a config client Backend that performs formatting | 5 // Package format implements a config client Backend that performs formatting |
| 6 // on items. | 6 // on items. |
| 7 // | 7 // |
| 8 // The available formats are registered during init() time via 'Register'. | 8 // The available formats are registered during init() time via 'Register'. |
| 9 package format | 9 package format |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // Get implements backend.B. | 30 // Get implements backend.B. |
| 31 func (b *Backend) Get(c context.Context, configSet, path string, p backend.Param
s) (*backend.Item, error) { | 31 func (b *Backend) Get(c context.Context, configSet, path string, p backend.Param
s) (*backend.Item, error) { |
| 32 item, err := b.B.Get(c, configSet, path, p) | 32 item, err := b.B.Get(c, configSet, path, p) |
| 33 if err != nil { | 33 if err != nil { |
| 34 return nil, err | 34 return nil, err |
| 35 } | 35 } |
| 36 | 36 |
| 37 if !p.FormatSpec.Unformatted() { | 37 if !p.FormatSpec.Unformatted() { |
| 38 formatter, err := getFormatter(p.FormatSpec.Formatter) | 38 formatter, err := getFormatter(p.FormatSpec.Formatter) |
| 39 if err != nil { | 39 if err != nil { |
| 40 » » » return nil, errors.Annotate(err).Reason("failed to get f
ormatter for %(format)q"). | 40 » » » return nil, errors.Annotate(err, "failed to get formatte
r for %q", p.FormatSpec).Err() |
| 41 » » » » D("format", p.FormatSpec).Err() | |
| 42 } | 41 } |
| 43 | 42 |
| 44 if err := b.formatItem(item, formatter, p.FormatSpec); err != ni
l { | 43 if err := b.formatItem(item, formatter, p.FormatSpec); err != ni
l { |
| 45 » » » return nil, errors.Annotate(err).Reason("failed to forma
t item to %(format)q, data %(data)q"). | 44 » » » return nil, errors.Annotate(err, "failed to format item
to %q, data %q", |
| 46 » » » » D("format", p.FormatSpec.Formatter). | 45 » » » » p.FormatSpec.Formatter, p.FormatSpec.Data).Err() |
| 47 » » » » D("data", p.FormatSpec.Data).Err() | |
| 48 } | 46 } |
| 49 } | 47 } |
| 50 return item, nil | 48 return item, nil |
| 51 } | 49 } |
| 52 | 50 |
| 53 // GetAll implements backend.B. | 51 // GetAll implements backend.B. |
| 54 func (b *Backend) GetAll(c context.Context, t backend.GetAllTarget, path string,
p backend.Params) ( | 52 func (b *Backend) GetAll(c context.Context, t backend.GetAllTarget, path string,
p backend.Params) ( |
| 55 []*backend.Item, error) { | 53 []*backend.Item, error) { |
| 56 | 54 |
| 57 items, err := b.B.GetAll(c, t, path, p) | 55 items, err := b.B.GetAll(c, t, path, p) |
| 58 if err != nil { | 56 if err != nil { |
| 59 return nil, err | 57 return nil, err |
| 60 } | 58 } |
| 61 | 59 |
| 62 if !p.FormatSpec.Unformatted() { | 60 if !p.FormatSpec.Unformatted() { |
| 63 formatter, err := getFormatter(p.FormatSpec.Formatter) | 61 formatter, err := getFormatter(p.FormatSpec.Formatter) |
| 64 if err != nil { | 62 if err != nil { |
| 65 » » » return nil, errors.Annotate(err).Reason("failed to get f
ormatter for %(format)q, data %(data)q"). | 63 » » » return nil, errors.Annotate(err, "failed to get formatte
r for %q, data %q", |
| 66 » » » » D("format", p.FormatSpec.Formatter). | 64 » » » » p.FormatSpec.Formatter, p.FormatSpec.Data).Err() |
| 67 » » » » D("data", p.FormatSpec.Data).Err() | |
| 68 } | 65 } |
| 69 | 66 |
| 70 lme := errors.NewLazyMultiError(len(items)) | 67 lme := errors.NewLazyMultiError(len(items)) |
| 71 for i, item := range items { | 68 for i, item := range items { |
| 72 if err := b.formatItem(item, formatter, p.FormatSpec); e
rr != nil { | 69 if err := b.formatItem(item, formatter, p.FormatSpec); e
rr != nil { |
| 73 lme.Assign(i, err) | 70 lme.Assign(i, err) |
| 74 } | 71 } |
| 75 } | 72 } |
| 76 | 73 |
| 77 if err := lme.Get(); err != nil { | 74 if err := lme.Get(); err != nil { |
| 78 » » » return nil, errors.Annotate(err).Reason("failed to forma
t items to %(format)q, data %(data)q"). | 75 » » » return nil, errors.Annotate(err, "failed to format items
to %q, data %q", |
| 79 » » » » D("format", p.FormatSpec.Formatter). | 76 » » » » p.FormatSpec.Formatter, p.FormatSpec.Data).Err() |
| 80 » » » » D("data", p.FormatSpec.Data).Err() | |
| 81 } | 77 } |
| 82 } | 78 } |
| 83 return items, nil | 79 return items, nil |
| 84 } | 80 } |
| 85 | 81 |
| 86 func (b *Backend) formatItem(it *backend.Item, formatter Formatter, fs backend.F
ormatSpec) error { | 82 func (b *Backend) formatItem(it *backend.Item, formatter Formatter, fs backend.F
ormatSpec) error { |
| 87 if !it.FormatSpec.Unformatted() { | 83 if !it.FormatSpec.Unformatted() { |
| 88 // Item is already formatted. | 84 // Item is already formatted. |
| 89 return nil | 85 return nil |
| 90 } | 86 } |
| 91 | 87 |
| 92 // Item is not formatted, so format it. | 88 // Item is not formatted, so format it. |
| 93 content, err := formatter.FormatItem(it.Content, fs.Data) | 89 content, err := formatter.FormatItem(it.Content, fs.Data) |
| 94 if err != nil { | 90 if err != nil { |
| 95 » » return errors.Annotate(err).Reason("failed to format item").Err(
) | 91 » » return errors.Annotate(err, "failed to format item").Err() |
| 96 } | 92 } |
| 97 it.Content = content | 93 it.Content = content |
| 98 it.FormatSpec = fs | 94 it.FormatSpec = fs |
| 99 return nil | 95 return nil |
| 100 } | 96 } |
| OLD | NEW |