Skip to content

Commit be2f35f

Browse files
Merge pull request #294 from sdcio/fixValidatorConfig
Fix Validator Config
2 parents 4d3b6b5 + ce950fc commit be2f35f

File tree

8 files changed

+59
-22
lines changed

8 files changed

+59
-22
lines changed

pkg/config/validation.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
package config
22

3+
func NewValidationConfig() *Validation {
4+
return &Validation{
5+
DisabledValidators: &Validators{},
6+
DisableConcurrency: bool(false),
7+
}
8+
}
9+
310
type Validation struct {
4-
DisabledValidators Validators `yaml:"disabled-validators,omitempty" json:"disabled-validators,omitempty"`
5-
DisableConcurrency bool `yaml:"disable-concurrency,omitempty" json:"disable-concurrency,omitempty"`
11+
DisabledValidators *Validators `yaml:"disabled-validators,omitempty" json:"disabled-validators,omitempty"`
12+
DisableConcurrency bool `yaml:"disable-concurrency,omitempty" json:"disable-concurrency,omitempty"`
613
}
714

815
func (v *Validation) validateSetDefaults() error {
16+
// no change required, all the bools default to false
17+
if v.DisabledValidators == nil {
18+
v.DisabledValidators = &Validators{}
19+
}
920
return nil
1021
}
1122

23+
func (v *Validation) SetDisableConcurrency(b bool) {
24+
v.DisableConcurrency = b
25+
}
26+
27+
func (v *Validation) DeepCopy() *Validation {
28+
return &Validation{
29+
DisabledValidators: v.DisabledValidators.DeepCopy(),
30+
DisableConcurrency: v.DisableConcurrency,
31+
}
32+
}
33+
1234
type Validators struct {
1335
Mandatory bool `yaml:"mandatory,omitempty" json:"mandatory,omitempty"`
1436
Leafref bool `yaml:"leafref,omitempty" json:"leafref,omitempty"`
@@ -30,3 +52,16 @@ func (v *Validators) DisableAll() {
3052
v.Pattern = true
3153
v.Range = true
3254
}
55+
56+
func (v *Validators) DeepCopy() *Validators {
57+
return &Validators{
58+
Mandatory: v.Mandatory,
59+
Leafref: v.Leafref,
60+
LeafrefMinMaxAttributes: v.LeafrefMinMaxAttributes,
61+
Pattern: v.Pattern,
62+
MustStatement: v.MustStatement,
63+
Length: v.Length,
64+
Range: v.Range,
65+
MaxElements: v.MaxElements,
66+
}
67+
}

pkg/datastore/transaction_rpc.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/sdcio/data-server/pkg/config"
1110
"github.com/sdcio/data-server/pkg/datastore/types"
1211
"github.com/sdcio/data-server/pkg/tree"
1312
treeproto "github.com/sdcio/data-server/pkg/tree/importer/proto"
@@ -23,10 +22,6 @@ var (
2322
ErrValidationError = errors.New("validation error")
2423
)
2524

26-
const (
27-
ConcurrentValidate = false
28-
)
29-
3025
// SdcpbTransactionIntentToInternalTI converts sdcpb.TransactionIntent to types.TransactionIntent
3126
func (d *Datastore) SdcpbTransactionIntentToInternalTI(ctx context.Context, req *sdcpb.TransactionIntent) (*types.TransactionIntent, error) {
3227

@@ -96,7 +91,7 @@ func (d *Datastore) replaceIntent(ctx context.Context, transaction *types.Transa
9691
log.TraceFn(func() []interface{} { return []interface{}{root.String()} })
9792

9893
// perform validation
99-
validationResult := root.Validate(ctx, &config.Validation{DisableConcurrency: !ConcurrentValidate})
94+
validationResult := root.Validate(ctx, d.config.Validation)
10095
validationResult.ErrorsStr()
10196
if validationResult.HasErrors() {
10297
return nil, validationResult.JoinErrors()
@@ -126,7 +121,7 @@ func (d *Datastore) replaceIntent(ctx context.Context, transaction *types.Transa
126121
func (d *Datastore) LoadAllButRunningIntents(ctx context.Context, root *tree.RootEntry) ([]string, error) {
127122

128123
intentNames := []string{}
129-
IntentChan := make(chan *tree_persist.Intent, 0)
124+
IntentChan := make(chan *tree_persist.Intent)
130125
ErrChan := make(chan error, 1)
131126

132127
go d.cacheClient.IntentGetAll(ctx, []string{"running"}, IntentChan, ErrChan)
@@ -223,7 +218,7 @@ func (d *Datastore) lowlevelTransactionSet(ctx context.Context, transaction *typ
223218
log.Debug(root.String())
224219

225220
// perform validation
226-
validationResult := root.Validate(ctx, &config.Validation{DisableConcurrency: !ConcurrentValidate})
221+
validationResult := root.Validate(ctx, d.config.Validation)
227222

228223
// prepare the response struct
229224
result := &sdcpb.TransactionSetResponse{

pkg/datastore/tree_operation_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ import (
3535

3636
var (
3737
// TypedValue Bool True and false
38-
TypedValueTrue = &sdcpb.TypedValue{Value: &sdcpb.TypedValue_BoolVal{BoolVal: true}}
39-
TypedValueFalse = &sdcpb.TypedValue{Value: &sdcpb.TypedValue_BoolVal{BoolVal: false}}
38+
TypedValueTrue = &sdcpb.TypedValue{Value: &sdcpb.TypedValue_BoolVal{BoolVal: true}}
39+
TypedValueFalse = &sdcpb.TypedValue{Value: &sdcpb.TypedValue_BoolVal{BoolVal: false}}
40+
validationConfig = config.NewValidationConfig()
4041
)
4142

43+
func init() {
44+
validationConfig.SetDisableConcurrency(true)
45+
}
46+
4247
func TestDatastore_populateTree(t *testing.T) {
4348
prio15 := int32(15)
4449
prio10 := int32(10)
@@ -818,7 +823,7 @@ func TestDatastore_populateTree(t *testing.T) {
818823
}
819824
fmt.Println(root.String())
820825

821-
validationResult := root.Validate(ctx, &config.Validation{DisableConcurrency: true})
826+
validationResult := root.Validate(ctx, validationConfig)
822827

823828
fmt.Printf("Validation Errors:\n%v\n", strings.Join(validationResult.ErrorsStr(), "\n"))
824829
fmt.Printf("Tree:%s\n", root.String())

pkg/datastore/tree_operation_validation_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/openconfig/ygot/ygot"
2525
"github.com/sdcio/data-server/pkg/cache"
26-
"github.com/sdcio/data-server/pkg/config"
2726
schemaClient "github.com/sdcio/data-server/pkg/datastore/clients/schema"
2827
"github.com/sdcio/data-server/pkg/tree"
2928
json_importer "github.com/sdcio/data-server/pkg/tree/importer/json"
@@ -206,7 +205,7 @@ func TestDatastore_validateTree(t *testing.T) {
206205
t.Error(err)
207206
}
208207

209-
validationResult := root.Validate(ctx, &config.Validation{DisableConcurrency: true})
208+
validationResult := root.Validate(ctx, validationConfig)
210209

211210
t.Log(root.String())
212211

pkg/server/datastore.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ func (s *Server) CreateDataStore(ctx context.Context, req *sdcpb.CreateDataStore
132132
Vendor: req.GetSchema().GetVendor(),
133133
Version: req.GetSchema().GetVersion(),
134134
},
135-
SBI: sbi,
135+
SBI: sbi,
136+
Validation: s.config.Validation.DeepCopy(),
136137
}
137138
if req.GetSync() != nil {
138139
dsConfig.Sync = &config.Sync{

pkg/tree/default_value_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestDefaultValueRetrieve(t *testing.T) {
149149
return rsp.GetSchema()
150150
},
151151
wanterr: false,
152-
want: types.NewUpdate(types.PathSlice{}, &sdcpb.TypedValue{Value: &sdcpb.TypedValue_LeaflistVal{LeaflistVal: &sdcpb.ScalarArray{Element: []*sdcpb.TypedValue{&sdcpb.TypedValue{Value: &sdcpb.TypedValue_StringVal{StringVal: "foo"}}, &sdcpb.TypedValue{Value: &sdcpb.TypedValue_StringVal{StringVal: "bar"}}}}}}, 5, "owner1", 0),
152+
want: types.NewUpdate(types.PathSlice{}, &sdcpb.TypedValue{Value: &sdcpb.TypedValue_LeaflistVal{LeaflistVal: &sdcpb.ScalarArray{Element: []*sdcpb.TypedValue{{Value: &sdcpb.TypedValue_StringVal{StringVal: "foo"}}, {Value: &sdcpb.TypedValue_StringVal{StringVal: "bar"}}}}}}, 5, "owner1", 0),
153153
},
154154
}
155155
for _, tt := range tests {

pkg/tree/entry_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import (
1818
var (
1919
flagsNew *types.UpdateInsertFlags
2020
flagsExisting *types.UpdateInsertFlags
21-
validationConfig = &config.Validation{DisableConcurrency: true}
21+
validationConfig = config.NewValidationConfig()
2222
)
2323

2424
func init() {
2525
flagsNew = types.NewUpdateInsertFlags()
2626
flagsNew.SetNewFlag()
2727
flagsExisting = types.NewUpdateInsertFlags()
28+
validationConfig.SetDisableConcurrency(true)
2829
}
2930

3031
func Test_Entry(t *testing.T) {

pkg/tree/sharedEntryAttributes_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,12 @@ func Test_sharedEntryAttributes_validateMandatory(t *testing.T) {
716716
t.Run(tt.name, func(t *testing.T) {
717717
root := tt.r(t)
718718

719-
dv := &config.Validators{}
720-
dv.DisableAll()
721-
dv.Mandatory = false
719+
validationConfig := config.NewValidationConfig()
720+
validationConfig.SetDisableConcurrency(true)
721+
validationConfig.DisabledValidators.DisableAll()
722+
validationConfig.DisabledValidators.Mandatory = false
722723

723-
validationResults := root.Validate(ctx, &config.Validation{DisableConcurrency: true, DisabledValidators: *dv})
724+
validationResults := root.Validate(ctx, validationConfig)
724725

725726
results := []string{}
726727
for _, e := range validationResults {

0 commit comments

Comments
 (0)