Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Unified Diff: luci_config/server/cfgclient/backend/format/format.go

Issue 2580713002: Implement a server-side config service interface. (Closed)
Patch Set: Renamed, moved to luci_config package, fixes, split out backends. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: luci_config/server/cfgclient/backend/format/format.go
diff --git a/luci_config/server/cfgclient/backend/format/format.go b/luci_config/server/cfgclient/backend/format/format.go
new file mode 100644
index 0000000000000000000000000000000000000000..d537a5df6f61be48073b039fe1b0afc54748217a
--- /dev/null
+++ b/luci_config/server/cfgclient/backend/format/format.go
@@ -0,0 +1,119 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+// Package format implements a config client Backend that performs formatting
+// on items.
+//
+// This must be used in conjuncton with a cfgclient.FormatterRegistry to
+// register Formatter instances.
+package format
+
+import (
+ "github.com/luci/luci-go/common/errors"
+ "github.com/luci/luci-go/luci_config/server/cfgclient"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
+
+ "golang.org/x/net/context"
+)
+
+// Backend is a backend.B implementation that applies Formatter
+// transformations to the various Items that pass through it.
+//
+// Formatter transformations are registered explicitly to the Resolver
+// descriptions of the types that they operate on. If an Item is already
+// formatted, no further transformations will be applied.
+type Backend struct {
+ // B is the underlying Backend that this Backend will pull data
+ // from.
+ backend.B
+
+ // GetRegistry returns the FormatterRegistry to use. If it returns nil,
+ // no formatting will be done.
+ GetRegistry func(context.Context) *cfgclient.FormatterRegistry
+}
+
+// Get implements backend.B.
+func (b *Backend) Get(c context.Context, configSet, path string, p backend.Params) (*backend.Item, error) {
+ item, err := b.B.Get(c, configSet, path, p)
+ if err != nil {
+ return nil, err
+ }
+
+ if !p.FormatSpec.Unformatted() {
+ formatter, err := b.getFormatter(c, p.FormatSpec.Formatter)
+ if err != nil {
+ return nil, errors.Annotate(err).Reason("failed to get formatter for %(format)q").
+ D("format", p.FormatSpec).Err()
+ }
+
+ if err := b.formatItem(item, formatter, p.FormatSpec); err != nil {
+ return nil, errors.Annotate(err).Reason("failed to format item to %(format)q, data %(data)q").
+ D("format", p.FormatSpec).Err()
+ }
+ }
+ return item, nil
+}
+
+// GetAll implements backend.B.
+func (b *Backend) GetAll(c context.Context, t backend.GetAllTarget, path string, p backend.Params) (
+ []*backend.Item, error) {
+
+ items, err := b.B.GetAll(c, t, path, p)
+ if err != nil {
+ return nil, err
+ }
+
+ if !p.FormatSpec.Unformatted() {
+ formatter, err := b.getFormatter(c, p.FormatSpec.Formatter)
+ if err != nil {
+ return nil, errors.Annotate(err).Reason("failed to get formatter for %(format)q, data %(data)q").
+ D("format", p.FormatSpec).Err()
+ }
+
+ lme := errors.NewLazyMultiError(len(items))
+ for i, item := range items {
+ if err := b.formatItem(item, formatter, p.FormatSpec); err != nil {
+ lme.Assign(i, err)
+ }
+ }
+
+ if err := lme.Get(); err != nil {
+ return nil, errors.Annotate(err).Reason("failed to format items to %(format)q, data %(data)q").
+ D("format", p.FormatSpec).Err()
+ }
+ }
+ return items, nil
+}
+
+func (b *Backend) getFormatter(c context.Context, f string) (cfgclient.Formatter, error) {
+ if b.GetRegistry == nil {
+ return nil, errors.New("no formatter registry function installed")
+ }
+
+ reg := b.GetRegistry(c)
+ if reg == nil {
+ return nil, errors.New("formatter registry function returned nil registry")
+ }
+ formatter := reg.Get(f)
+ if formatter == nil {
+ return nil, errors.Reason("unknown formatter: %(formatter)q").D("formatter", f).Err()
+ }
+ return formatter, nil
+}
+
+func (b *Backend) formatItem(it *backend.Item, formatter cfgclient.Formatter, fs backend.FormatSpec) error {
+ if !it.FormatSpec.Unformatted() {
+ // Item is already formatted.
+ return nil
+ }
+
+ // Item is not formatted, so format it.
+ content, err := formatter.FormatItem(it.Content, fs.Data)
+ if err != nil {
+ return errors.Annotate(err).Reason("failed to format item").Err()
+ }
+ it.Content = content
+ it.FormatSpec = fs
+ return nil
+}
« no previous file with comments | « luci_config/server/cfgclient/backend/doc.go ('k') | luci_config/server/cfgclient/backend/format/format_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698