aboutsummaryrefslogtreecommitdiff
path: root/metadata/config.go
diff options
context:
space:
mode:
authorJoe Richey joerichey@google.com <joerichey@google.com>2017-05-23 18:38:38 -0700
committerJoe Richey joerichey@google.com <joerichey@google.com>2017-05-31 12:35:28 -0700
commit44c2c7aeda3de09a405ed06aadacbc2c0c7f2a67 (patch)
tree74bd0fccf5c23a739b434893e1d02c16e0153fca /metadata/config.go
parent4b6d0ce14b8553a93b2d14fd858dfd35bfd61104 (diff)
metadata: reorganize and add consistency checks
This commit adds in IsValid() checks for the metadata structures that let us enforce stronger invariants than those imposed by the protobuf package. The main uses of this will be to check that metadata is valid before writing it to the filesystem, and to check that the filesystem contains valid metadata before returning it to the user. These functions also will log the exact reason if the validity checks fail. To have these checks in the metadata package, all of the various constants have been moved to a single metadata/constants.go file. The uses of these constants were changed accordingly. Finally, this commit standardizes our use of errors so that they always begin with an appropriate prefix. Change-Id: I99008e2ee803ebe5f6236eb8d83fc83efcd22718
Diffstat (limited to 'metadata/config.go')
-rw-r--r--metadata/config.go41
1 files changed, 34 insertions, 7 deletions
diff --git a/metadata/config.go b/metadata/config.go
index 47b6cce..8c7be55 100644
--- a/metadata/config.go
+++ b/metadata/config.go
@@ -19,7 +19,7 @@
*/
// Package metadata contains all of the on disk structures.
-// These structures are definied in meatadata.proto. The package also
+// These structures are defined in metadata.proto. The package also
// contains functions for manipulating these structures, specifically:
// * Reading and Writing the Config file to disk
// * Getting and Setting Policies for directories
@@ -27,21 +27,48 @@
package metadata
//go:generate protoc --go_out=. metadata.proto
-import "github.com/golang/protobuf/jsonpb"
+import (
+ "io"
+ "strings"
+
+ "github.com/golang/protobuf/jsonpb"
+)
// WriteConfig outputs the Config data as nicely formatted JSON
-func WriteConfig(config *Config) (string, error) {
+func WriteConfig(config *Config, out io.Writer) error {
m := jsonpb.Marshaler{
- EmitDefaults: false,
+ EmitDefaults: true,
EnumsAsInts: false,
Indent: "\t",
OrigName: true,
}
- return m.MarshalToString(config)
+ if err := m.Marshal(out, config); err != nil {
+ return err
+ }
+
+ _, err := out.Write([]byte{'\n'})
+ return err
}
// ReadConfig writes the JSON data into the config structure
-func ReadConfig(input string) (*Config, error) {
+func ReadConfig(in io.Reader) (*Config, error) {
config := new(Config)
- return config, jsonpb.UnmarshalString(input, config)
+ // Allow (and ignore) unknown fields for forwards compatibility.
+ u := jsonpb.Unmarshaler{
+ AllowUnknownFields: true,
+ }
+ return config, u.Unmarshal(in, config)
+}
+
+// HasCompatibilityOption returns true if the specified string is in the list of
+// compatibility options. This assumes the compatibility options are in a comma
+// separated string.
+func (c *Config) HasCompatibilityOption(option string) bool {
+ options := strings.Split(c.Compatibility, ",")
+ for _, o := range options {
+ if o == option {
+ return true
+ }
+ }
+ return false
}