Skip to content

Commit 3520930

Browse files
authored
Add ability to disable validation (#213)
* Add ability to disable validation. * Update to go.mod to 1.17 minimum to match the go version installation. * Pin go-licence-detector to v0.8.0 to work with go 1.17. * Add changelog entry.
1 parent 2a22403 commit 3520930

File tree

6 files changed

+75
-35
lines changed

6 files changed

+75
-35
lines changed

.ci/check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ go install github.com/elastic/go-licenser@latest
2121
checkformat go-licenser -license ASL2
2222

2323
echo "Check notice file"
24-
go install go.elastic.co/go-licence-detector@latest
24+
go install go.elastic.co/go-licence-detector@v0.8.0
2525
checkformat dev-tools/generate_notice

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
## [Unreleased]
66

77
### Added
8+
- Add ability to skip validation with new `NoValidate` option. #213
89

910
### Changed
1011

go.mod

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
module github.com/elastic/go-ucfg
22

3-
go 1.13
3+
go 1.17
44

55
require (
66
github.com/davecgh/go-spew v1.1.1
7-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
87
github.com/stretchr/testify v1.4.0
9-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
108
gopkg.in/hjson/hjson-go.v3 v3.0.1
119
gopkg.in/yaml.v2 v2.2.8
1210
)
11+
12+
require (
13+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
16+
)

opts.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Option func(*options)
3636
type options struct {
3737
tag string
3838
validatorTag string
39+
noValidate bool
3940
pathSep string
4041
escapePath bool
4142
meta *Meta
@@ -100,6 +101,13 @@ func ValidatorTag(tag string) Option {
100101
}
101102
}
102103

104+
// NoValidate disables validation when unpacking.
105+
func NoValidate() Option {
106+
return func(o *options) {
107+
o.noValidate = true
108+
}
109+
}
110+
103111
// PathSep sets the path separator used to split up names into a tree like hierarchy.
104112
// If PathSep is not set, field names will not be split.
105113
func PathSep(sep string) Option {

reify.go

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,10 @@ func reifyMap(opts *options, to reflect.Value, from *Config, validators []valida
203203

204204
fields := from.fields.dict()
205205
if len(fields) == 0 {
206-
if err := tryRecursiveValidate(to, opts, validators); err != nil {
207-
return raiseValidation(from.ctx, from.metadata, "", err)
206+
if !opts.noValidate {
207+
if err := tryRecursiveValidate(to, opts, validators); err != nil {
208+
return raiseValidation(from.ctx, from.metadata, "", err)
209+
}
208210
}
209211
return nil
210212
}
@@ -231,11 +233,13 @@ func reifyMap(opts *options, to reflect.Value, from *Config, validators []valida
231233
}
232234
}
233235

234-
if err := runValidators(to.Interface(), validators); err != nil {
235-
return raiseValidation(from.ctx, from.metadata, "", err)
236-
}
237-
if err := tryValidate(to); err != nil {
238-
return raiseValidation(from.ctx, from.metadata, "", err)
236+
if !opts.noValidate {
237+
if err := runValidators(to.Interface(), validators); err != nil {
238+
return raiseValidation(from.ctx, from.metadata, "", err)
239+
}
240+
if err := tryValidate(to); err != nil {
241+
return raiseValidation(from.ctx, from.metadata, "", err)
242+
}
239243
}
240244

241245
return nil
@@ -296,8 +300,10 @@ func reifyStruct(opts *options, orig reflect.Value, cfg *Config) Error {
296300
}
297301
}
298302

299-
if err := tryValidate(to); err != nil {
300-
return raiseValidation(cfg.ctx, cfg.metadata, "", err)
303+
if !opts.noValidate {
304+
if err := tryValidate(to); err != nil {
305+
return raiseValidation(cfg.ctx, cfg.metadata, "", err)
306+
}
301307
}
302308

303309
orig.Set(pointerize(orig.Type(), to.Type(), to))
@@ -324,16 +330,20 @@ func reifyGetField(
324330
// When fieldType is a pointer and the value is nil, return nil as the
325331
// underlying type should not be allocated.
326332
if fieldType.Kind() == reflect.Ptr {
327-
if err := tryRecursiveValidate(to, opts.opts, opts.validators); err != nil {
328-
return raiseValidation(cfg.ctx, cfg.metadata, name, err)
333+
if !opts.opts.noValidate {
334+
if err := tryRecursiveValidate(to, opts.opts, opts.validators); err != nil {
335+
return raiseValidation(cfg.ctx, cfg.metadata, name, err)
336+
}
329337
}
330338
return nil
331339
}
332340

333341
// Primitive types return early when it doesn't implement the Initializer interface.
334342
if fieldType.Kind() != reflect.Struct && !hasInitDefaults(fieldType) {
335-
if err := tryRecursiveValidate(to, opts.opts, opts.validators); err != nil {
336-
return raiseValidation(cfg.ctx, cfg.metadata, name, err)
343+
if !opts.opts.noValidate {
344+
if err := tryRecursiveValidate(to, opts.opts, opts.validators); err != nil {
345+
return raiseValidation(cfg.ctx, cfg.metadata, name, err)
346+
}
337347
}
338348
return nil
339349
}
@@ -604,21 +614,23 @@ func reifyDoArray(
604614
if v.IsValid() {
605615
to.Index(idx).Set(v)
606616
}
607-
} else {
617+
} else if !opts.opts.noValidate {
608618
if err := tryRecursiveValidate(to.Index(idx), opts.opts, nil); err != nil {
609619
return reflect.Value{}, raiseValidation(val.Context(), val.meta(), "", err)
610620
}
611621
}
612622
}
613623

614-
if err := runValidators(to.Interface(), opts.validators); err != nil {
615-
ctx := val.Context()
616-
return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err)
617-
}
624+
if !opts.opts.noValidate {
625+
if err := runValidators(to.Interface(), opts.validators); err != nil {
626+
ctx := val.Context()
627+
return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err)
628+
}
618629

619-
if err := tryValidate(to); err != nil {
620-
ctx := val.Context()
621-
return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err)
630+
if err := tryValidate(to); err != nil {
631+
ctx := val.Context()
632+
return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err)
633+
}
622634
}
623635

624636
return to, nil
@@ -679,12 +691,14 @@ func reifyPrimitive(
679691
}
680692
}
681693

682-
if err := runValidators(v.Interface(), opts.validators); err != nil {
683-
return reflect.Value{}, raiseValidation(val.Context(), val.meta(), "", err)
684-
}
694+
if !opts.opts.noValidate {
695+
if err := runValidators(v.Interface(), opts.validators); err != nil {
696+
return reflect.Value{}, raiseValidation(val.Context(), val.meta(), "", err)
697+
}
685698

686-
if err := tryValidate(v); err != nil {
687-
return reflect.Value{}, raiseValidation(val.Context(), val.meta(), "", err)
699+
if err := tryValidate(v); err != nil {
700+
return reflect.Value{}, raiseValidation(val.Context(), val.meta(), "", err)
701+
}
688702
}
689703

690704
return pointerize(t, baseType, chaseValuePointers(v)), nil

validator_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
"github.com/davecgh/go-spew/spew"
2728
"github.com/elastic/go-ucfg/cfgtest"
2829
)
2930

@@ -531,11 +532,23 @@ func TestValidationFail(t *testing.T) {
531532
},
532533
}
533534

534-
for i, test := range tests {
535-
t.Run(fmt.Sprintf("Test config (%v): %#v", i, test), func(t *testing.T) {
536-
cfgtest.MustFailUnpack(t, c, test)
537-
})
538-
}
535+
t.Run("validate", func(t *testing.T) {
536+
for i, test := range tests {
537+
t.Run(fmt.Sprintf("Test config (%v): %#v", i, test), func(t *testing.T) {
538+
cfgtest.MustFailUnpack(t, c, test)
539+
})
540+
}
541+
})
542+
t.Run("noValidate", func(t *testing.T) {
543+
for i, test := range tests {
544+
t.Run(fmt.Sprintf("Test config (%v): %#v", i, test), func(t *testing.T) {
545+
err := c.Unpack(test, NoValidate())
546+
if err != nil {
547+
t.Fatalf("config:%s test:%s error:%v", spew.Sdump(c), spew.Sdump(test), err)
548+
}
549+
})
550+
}
551+
})
539552
}
540553

541554
func TestValidateRequiredFailing(t *testing.T) {

0 commit comments

Comments
 (0)