summaryrefslogtreecommitdiff
path: root/vendor/gopkg.in/go-playground/validator.v9/validator_instance.go
diff options
context:
space:
mode:
authorDan Duvall <dduvall@wikimedia.org>2018-03-06 20:31:58 -0800
committerDan Duvall <dduvall@wikimedia.org>2018-03-19 15:55:16 -0700
commiteb9b69dd3d710cb7afa1dfb6e23a5987842b21cc (patch)
tree049b11cc885e4e9f54aac8981c91a1bf3620e7af /vendor/gopkg.in/go-playground/validator.v9/validator_instance.go
parent6896e655eb5cc88b90e66979bc2d862eb92cbb9f (diff)
downloadblubber-eb9b69dd3d710cb7afa1dfb6e23a5987842b21cc.tar.gz
Allow for configuration policies
Summary: Implements a rough interface for validating configuration against arbitrary policy rules. Policies are provided as YAML and passed via the command line as file paths or remote URIs. The format of policies is: enforcements: - path: <path> rule: <rule> Where `<path>` is a YAML-ish path to a config field and `<rule>` is any expression our config validator understands (expressions built in by the validator library and custom tags defined in `config.validation.go`). Example policy: enforcements: - path: variants.production.base rule: oneof=debian:jessie debian:stretch - path: variants.production.runs.as rule: ne=foo - path: variants.production.node.dependencies rule: isfalse Command flag parsing was implemented in `main.go` to support the new `--policy=uri` flag and improve existing handling of `--version` and the usage statement. Test Plan: Run `go test ./...`. Reviewers: thcipriani, demon, hashar, mmodell, #release-engineering-team Reviewed By: thcipriani, #release-engineering-team Tags: #release-engineering-team Differential Revision: https://phabricator.wikimedia.org/D999
Diffstat (limited to 'vendor/gopkg.in/go-playground/validator.v9/validator_instance.go')
-rw-r--r--vendor/gopkg.in/go-playground/validator.v9/validator_instance.go85
1 files changed, 23 insertions, 62 deletions
diff --git a/vendor/gopkg.in/go-playground/validator.v9/validator_instance.go b/vendor/gopkg.in/go-playground/validator.v9/validator_instance.go
index d3a1543..e84b452 100644
--- a/vendor/gopkg.in/go-playground/validator.v9/validator_instance.go
+++ b/vendor/gopkg.in/go-playground/validator.v9/validator_instance.go
@@ -370,39 +370,37 @@ func (v *Validate) StructPartialCtx(ctx context.Context, s interface{}, fields .
typ := val.Type()
name := typ.Name()
- if fields != nil {
- for _, k := range fields {
+ for _, k := range fields {
- flds := strings.Split(k, namespaceSeparator)
- if len(flds) > 0 {
+ flds := strings.Split(k, namespaceSeparator)
+ if len(flds) > 0 {
- vd.misc = append(vd.misc[0:0], name...)
- vd.misc = append(vd.misc, '.')
-
- for _, s := range flds {
+ vd.misc = append(vd.misc[0:0], name...)
+ vd.misc = append(vd.misc, '.')
- idx := strings.Index(s, leftBracket)
+ for _, s := range flds {
- if idx != -1 {
- for idx != -1 {
- vd.misc = append(vd.misc, s[:idx]...)
- vd.includeExclude[string(vd.misc)] = struct{}{}
+ idx := strings.Index(s, leftBracket)
- idx2 := strings.Index(s, rightBracket)
- idx2++
- vd.misc = append(vd.misc, s[idx:idx2]...)
- vd.includeExclude[string(vd.misc)] = struct{}{}
- s = s[idx2:]
- idx = strings.Index(s, leftBracket)
- }
- } else {
+ if idx != -1 {
+ for idx != -1 {
+ vd.misc = append(vd.misc, s[:idx]...)
+ vd.includeExclude[string(vd.misc)] = struct{}{}
- vd.misc = append(vd.misc, s...)
+ idx2 := strings.Index(s, rightBracket)
+ idx2++
+ vd.misc = append(vd.misc, s[idx:idx2]...)
vd.includeExclude[string(vd.misc)] = struct{}{}
+ s = s[idx2:]
+ idx = strings.Index(s, leftBracket)
}
+ } else {
- vd.misc = append(vd.misc, '.')
+ vd.misc = append(vd.misc, s...)
+ vd.includeExclude[string(vd.misc)] = struct{}{}
}
+
+ vd.misc = append(vd.misc, '.')
}
}
}
@@ -520,36 +518,18 @@ func (v *Validate) VarCtx(ctx context.Context, field interface{}, tag string) (e
return nil
}
- // find cached tag
- ctag, ok := v.tagCache.Get(tag)
- if !ok {
- v.tagCache.lock.Lock()
- defer v.tagCache.lock.Unlock()
-
- // could have been multiple trying to access, but once first is done this ensures tag
- // isn't parsed again.
- ctag, ok = v.tagCache.Get(tag)
- if !ok {
- ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
- v.tagCache.Set(tag, ctag)
- }
- }
-
+ ctag := v.fetchCacheTag(tag)
val := reflect.ValueOf(field)
-
vd := v.pool.Get().(*validate)
vd.top = val
vd.isPartial = false
-
vd.traverseField(ctx, val, val, vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)
if len(vd.errs) > 0 {
err = vd.errs
vd.errs = nil
}
-
v.pool.Put(vd)
-
return
}
@@ -590,36 +570,17 @@ func (v *Validate) VarWithValueCtx(ctx context.Context, field interface{}, other
if len(tag) == 0 || tag == skipValidationTag {
return nil
}
-
- // find cached tag
- ctag, ok := v.tagCache.Get(tag)
- if !ok {
- v.tagCache.lock.Lock()
- defer v.tagCache.lock.Unlock()
-
- // could have been multiple trying to access, but once first is done this ensures tag
- // isn't parsed again.
- ctag, ok = v.tagCache.Get(tag)
- if !ok {
- ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
- v.tagCache.Set(tag, ctag)
- }
- }
-
+ ctag := v.fetchCacheTag(tag)
otherVal := reflect.ValueOf(other)
-
vd := v.pool.Get().(*validate)
vd.top = otherVal
vd.isPartial = false
-
vd.traverseField(ctx, otherVal, reflect.ValueOf(field), vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)
if len(vd.errs) > 0 {
err = vd.errs
vd.errs = nil
}
-
v.pool.Put(vd)
-
return
}