From fc22e628d9579caef02ebbaaf09a73c468d25896 Mon Sep 17 00:00:00 2001 From: wenovus Date: Thu, 1 Jun 2023 11:33:48 -0700 Subject: [PATCH] Support validating ordered maps --- .../schemaops/ctestschema/ctestschema.go | 188 +++++++++--------- .../schemaops/yang/ctestschema.yang | 3 + internal/yreflect/reflect_orderedmap.go | 6 +- ygot/render_test.go | 8 + ygot/struct_validation_map_test.go | 8 + ygot/types.go | 2 + ytypes/list.go | 35 ++-- ytypes/list_exported_test.go | 83 ++++++++ ytypes/list_test.go | 2 +- ytypes/util_schema.go | 9 +- ytypes/validate_test.go | 1 + 11 files changed, 233 insertions(+), 112 deletions(-) diff --git a/integration_tests/schemaops/ctestschema/ctestschema.go b/integration_tests/schemaops/ctestschema/ctestschema.go index c2b221db6..7329c8ff0 100644 --- a/integration_tests/schemaops/ctestschema/ctestschema.go +++ b/integration_tests/schemaops/ctestschema/ctestschema.go @@ -1141,100 +1141,100 @@ var ( // contents of a goyang yang.Entry struct, which defines the schema for the // fields within the struct. ySchema = []byte{ - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x5d, 0x51, 0x6f, 0xdb, 0x36, - 0x10, 0x7e, 0xf7, 0xaf, 0x10, 0xf4, 0xdc, 0x34, 0x91, 0xe7, 0xc4, 0x99, 0xdf, 0xb2, 0x66, 0xc5, - 0x80, 0xce, 0x5b, 0xd1, 0xae, 0x7b, 0x19, 0x8a, 0x41, 0x55, 0x68, 0x97, 0x88, 0x2d, 0x05, 0x12, - 0x95, 0xc5, 0x18, 0xfc, 0xdf, 0x07, 0x59, 0xb2, 0x66, 0xc9, 0x96, 0x7d, 0x47, 0x52, 0x8e, 0xe2, - 0x7c, 0x6f, 0x89, 0x44, 0x8a, 0xe2, 0xdd, 0x7d, 0xc7, 0xbb, 0x4f, 0x47, 0xfa, 0xdf, 0x9e, 0xe3, - 0x38, 0x8e, 0xfb, 0x9b, 0x3f, 0x17, 0xee, 0xc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0x57, 0x88, 0x6e, 0x0b, 0xc1, 0x6e, 0x17, 0x83, 0x5d, 0xb2, 0x9b, 0x29, - 0x25, 0x9d, 0x61, 0x31, 0x16, 0x13, 0x8a, 0xb4, 0xd7, 0x7e, 0x65, 0x48, 0x68, 0xfb, 0xb1, 0x80, - 0xc7, 0xdb, 0xb7, 0x85, 0xe5, 0x9f, 0x67, 0x96, 0xd7, 0x82, 0xf5, 0xef, 0x27, 0x52, 0x48, 0x89, - 0x7d, 0x82, 0xf4, 0x0f, 0xe9, 0x1f, 0x8d, 0x18, 0x32, 0x21, 0x8a, 0x34, 0xcd, 0x0a, 0xf1, 0xe1, - 0x6b, 0x88, 0x0f, 0xa9, 0x66, 0xca, 0x25, 0xba, 0xcc, 0x88, 0x2f, 0x43, 0x93, 0xd5, 0x36, 0x5d, - 0x13, 0x13, 0x36, 0x34, 0x65, 0x53, 0x93, 0xb6, 0x66, 0xda, 0xd6, 0x4c, 0xdc, 0xdc, 0xd4, 0x79, - 0x26, 0xcf, 0x34, 0x7d, 0x6d, 0x08, 0x68, 0x11, 0x77, 0x16, 0x88, 0x3c, 0xc3, 0x3c, 0xdf, 0x1a, - 0x38, 0x6c, 0x80, 0xc4, 0x12, 0x58, 0x6c, 0x81, 0xc6, 0x3a, 0x78, 0xac, 0x83, 0xc8, 0x1e, 0x98, - 0xf4, 0x40, 0xa5, 0x09, 0x2e, 0x7d, 0x1e, 0xc2, 0x1e, 0x2f, 0x61, 0xc8, 0x53, 0xd8, 0x92, 0x80, - 0x26, 0x8f, 0x61, 0x9f, 0xd7, 0x30, 0x37, 0x04, 0x0d, 0x11, 0x30, 0x89, 0x4f, 0x2b, 0x44, 0x28, - 0x1c, 0x26, 0x1c, 0x26, 0x1c, 0x26, 0x1c, 0x66, 0x07, 0x1c, 0x66, 0xab, 0x01, 0xac, 0xa1, 0xa0, - 0x34, 0x89, 0x65, 0x43, 0xce, 0x59, 0x5f, 0x44, 0x0c, 0xf1, 0x68, 0x85, 0xe8, 0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, 0x2e, 0xad, 0x33, - 0xa0, 0x4d, 0x37, 0xda, 0x3d, 0xcf, 0xd4, 0x63, 0x1d, 0x68, 0xea, 0xe1, 0x44, 0x53, 0x9c, 0x68, - 0xfa, 0x3c, 0x27, 0x9a, 0x7a, 0x2d, 0x01, 0xa0, 0xcf, 0x02, 0x40, 0x1f, 0x00, 0x00, 0x00, 0x9e, - 0x07, 0x00, 0x7d, 0x54, 0xbc, 0x02, 0x02, 0xf8, 0x74, 0x80, 0xcc, 0x08, 0x99, 0x51, 0x97, 0x32, - 0x23, 0x30, 0xf3, 0x40, 0x13, 0x98, 0x79, 0x30, 0xf3, 0x60, 0xe6, 0xc1, 0xcc, 0x77, 0x80, 0x99, - 0x07, 0xf1, 0x8d, 0x05, 0x09, 0xe1, 0xdd, 0x31, 0xc3, 0xbb, 0x17, 0xcb, 0x2b, 0x3f, 0xcb, 0xc6, - 0x0c, 0xcf, 0xd9, 0x13, 0xb5, 0x62, 0x7b, 0x46, 0xb3, 0x87, 0xc0, 0xf6, 0x8c, 0xfc, 0x01, 0xd8, - 0x9e, 0xd1, 0x1e, 0xc1, 0x74, 0xa2, 0xdb, 0x33, 0xa8, 0x1e, 0x91, 0xbd, 0x69, 0x63, 0x5c, 0xf6, - 0xef, 0xd4, 0xf6, 0x8d, 0xad, 0xbd, 0x15, 0x84, 0x9d, 0x1c, 0x69, 0x48, 0xfc, 0x9d, 0xfc, 0x7a, - 0x43, 0xec, 0xdb, 0x20, 0xab, 0xb6, 0x71, 0xdf, 0x46, 0x55, 0xa6, 0x87, 0xb7, 0x6b, 0xd4, 0xda, - 0x63, 0x97, 0x06, 0x76, 0x69, 0x58, 0x49, 0x9e, 0xf0, 0xa9, 0x05, 0x87, 0x8b, 0x20, 0x13, 0x47, - 0x26, 0x8e, 0x12, 0x34, 0xd4, 0x9c, 0x02, 0xf0, 0x00, 0x3c, 0x00, 0x7f, 0x22, 0xdc, 0x60, 0x2d, - 0x69, 0xab, 0xfd, 0x8f, 0x5f, 0xcc, 0x47, 0xc0, 0x8b, 0x5f, 0xcc, 0x47, 0x75, 0x1d, 0x10, 0x80, - 0x94, 0x0f, 0x11, 0x20, 0x22, 0xc0, 0xce, 0x44, 0x80, 0xc8, 0xa8, 0x80, 0x27, 0xe0, 0xe9, 0x98, - 0x78, 0x7a, 0x21, 0x09, 0x0b, 0xce, 0x94, 0x6c, 0xea, 0xbc, 0x59, 0xb4, 0x10, 0xa6, 0xb3, 0x19, - 0xe3, 0x53, 0x7c, 0x73, 0xbd, 0x5b, 0x8b, 0xdf, 0x68, 0xf7, 0x2b, 0x9a, 0xfe, 0x45, 0xf6, 0xcb, - 0xba, 0x5f, 0x27, 0xbe, 0xc4, 0xd6, 0x3f, 0x92, 0x36, 0x7d, 0x80, 0xed, 0x6d, 0x8c, 0xdb, 0x34, - 0x9e, 0x2b, 0x93, 0x77, 0x25, 0x45, 0xf1, 0x79, 0xf5, 0xfc, 0x2d, 0xff, 0xe8, 0xca, 0xe4, 0xbd, - 0x7f, 0x2f, 0x3e, 0x45, 0xd1, 0xb6, 0xef, 0xac, 0xbf, 0xa7, 0xbb, 0x79, 0xab, 0x22, 0xc3, 0x5b, - 0xf1, 0x28, 0x83, 0x02, 0x56, 0xcb, 0xde, 0xf2, 0x3f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x00, - 0x00, 0xff, 0xff, 0xee, 0x62, 0xb3, 0x96, 0x87, 0x9d, 0x00, 0x00, + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x5d, 0xdf, 0x6f, 0xa3, 0x46, + 0x10, 0x7e, 0xcf, 0x5f, 0x81, 0x78, 0xbe, 0x5c, 0x82, 0x9b, 0x5f, 0xf5, 0x5b, 0x7a, 0xe9, 0xa9, + 0xd2, 0x35, 0xed, 0xe9, 0xae, 0xd7, 0x97, 0xea, 0x54, 0x71, 0x64, 0xed, 0x43, 0xb1, 0x21, 0x82, + 0x25, 0x8d, 0x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, 0x20, 0x30, 0x17, + 0xe6, 0xa6, 0xc1, 0x77, 0x35, 0xf5, 0x1f, 0x7c, 0xfd, 0x3d, 0x57, 0xcf, 0x49, 0xa0, 0x55, 0xaa, + 0x8b, 0x6b, 0x27, 0xb5, 0x74, 0xad, 0xf6, 0xdf, 0x49, 0x19, 0x68, 0x1e, 0xc9, 0xd6, 0xb1, 0x65, + 0x0d, 0xa4, 0xb8, 0x85, 0x11, 0xaf, 0x10, 0xdd, 0x16, 0x82, 0xdd, 0x3e, 0x06, 0xbb, 0x64, 0x37, + 0x53, 0x49, 0x3a, 0xc7, 0x62, 0xa2, 0x46, 0x14, 0x69, 0x2f, 0xfd, 0xca, 0x25, 0x61, 0xec, 0xc7, + 0x12, 0x1e, 0x6f, 0xdf, 0x96, 0x96, 0x7f, 0x92, 0x5b, 0x5e, 0x07, 0xd6, 0xbf, 0x9d, 0x48, 0x21, + 0x25, 0xf6, 0x29, 0xd2, 0x3f, 0xa4, 0x7f, 0x34, 0x62, 0xc8, 0x84, 0x28, 0x12, 0x9a, 0x15, 0xe2, + 0xc3, 0x43, 0x88, 0x0f, 0xa9, 0x66, 0xca, 0x25, 0xba, 0xcc, 0x88, 0x2f, 0x43, 0x93, 0x15, 0x9b, + 0xae, 0x89, 0x09, 0x1b, 0x9a, 0xb2, 0xa9, 0x49, 0x5b, 0x33, 0x6d, 0x6b, 0x26, 0x6e, 0x6e, 0xea, + 0x3c, 0x93, 0x67, 0x9a, 0xbe, 0x18, 0x02, 0x22, 0xe2, 0xce, 0x02, 0x91, 0x67, 0x98, 0xe7, 0x5b, + 0x03, 0x87, 0x0d, 0x90, 0x58, 0x02, 0x8b, 0x2d, 0xd0, 0x58, 0x07, 0x8f, 0x75, 0x10, 0xd9, 0x03, + 0x93, 0x0c, 0x54, 0x42, 0x70, 0xc9, 0x79, 0x08, 0x7b, 0xbc, 0x84, 0x21, 0x4f, 0x61, 0x4b, 0x02, + 0x42, 0x1e, 0xc3, 0x3e, 0xaf, 0x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0xea, 0x32, + 0xdd, 0xdd, 0xae, 0xd1, 0x18, 0x8f, 0x2e, 0x0d, 0x74, 0x69, 0x58, 0x49, 0x9e, 0xf0, 0xaa, 0x05, + 0x27, 0x89, 0x20, 0x13, 0x47, 0x26, 0x8e, 0x12, 0x34, 0xd4, 0x9c, 0x02, 0xf0, 0x00, 0x3c, 0x00, + 0xff, 0x4a, 0xb8, 0xc1, 0x46, 0xd2, 0xd6, 0xf8, 0x1f, 0x3f, 0x8f, 0x8f, 0x80, 0x17, 0x3f, 0x8f, + 0x8f, 0xea, 0x3a, 0x20, 0x00, 0x29, 0x1f, 0x22, 0x40, 0x44, 0x80, 0xbd, 0x89, 0x00, 0x91, 0x51, + 0x01, 0x4f, 0xc0, 0xd3, 0x3e, 0xf1, 0xf4, 0x42, 0x12, 0x96, 0x03, 0x3a, 0x40, 0xd2, 0xa0, 0x68, + 0x21, 0xca, 0x26, 0x13, 0xc6, 0xab, 0xf8, 0xf6, 0x7a, 0xb7, 0x0e, 0xdf, 0xd1, 0x6e, 0x57, 0x34, + 0xfd, 0x8d, 0xec, 0x97, 0xe5, 0x7d, 0xbd, 0x78, 0x13, 0xdb, 0x7c, 0x49, 0xda, 0xf6, 0x02, 0xf6, + 0x68, 0x65, 0xde, 0xb6, 0xf9, 0xdc, 0x30, 0x7d, 0x57, 0x51, 0x14, 0x9f, 0x17, 0xdf, 0xbf, 0xe6, + 0x1f, 0xdd, 0x30, 0x7d, 0xef, 0xdf, 0xab, 0x4f, 0x71, 0xbc, 0xee, 0x3b, 0x9b, 0xcf, 0xe9, 0xae, + 0x7e, 0x54, 0x93, 0xe1, 0x8d, 0x7a, 0x0c, 0x83, 0x12, 0x56, 0xf3, 0xa3, 0xf9, 0x7f, 0x00, 0x00, + 0x00, 0xff, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff, 0xfb, 0x40, 0xea, 0x94, 0x74, 0x9d, 0x00, 0x00, } ) diff --git a/integration_tests/schemaops/yang/ctestschema.yang b/integration_tests/schemaops/yang/ctestschema.yang index 37e3d10b5..4d8860940 100644 --- a/integration_tests/schemaops/yang/ctestschema.yang +++ b/integration_tests/schemaops/yang/ctestschema.yang @@ -10,6 +10,9 @@ module ctestschema { list ordered-list { key "key"; ordered-by user; + // These numbers are for testing the validation logic in ytypes/. + min-elements 0; + max-elements 5; leaf key { type leafref { path "../config/key"; } } diff --git a/internal/yreflect/reflect_orderedmap.go b/internal/yreflect/reflect_orderedmap.go index 96ac7a940..821782491 100644 --- a/internal/yreflect/reflect_orderedmap.go +++ b/internal/yreflect/reflect_orderedmap.go @@ -27,6 +27,8 @@ type goOrderedList interface { // IsYANGOrderedList is a marker method that indicates that the struct // implements the goOrderedList interface. IsYANGOrderedList() + // Len returns the size of the ordered list. + Len() int } // AppendIntoOrderedMap appends a populated value into the ordered map. @@ -50,8 +52,8 @@ func AppendIntoOrderedMap(orderedMap goOrderedList, value interface{}) error { // RangeOrderedMap calls a visitor function over each key-value pair in order. // -// If the visit function returns false, the for loop breaks. -// An erorr is returned if the ordered map is not well-formed. +// The for loop break when either the visit function returns false or an error +// is encountered due to the ordered map not being well-formed. func RangeOrderedMap(orderedMap goOrderedList, visit func(k reflect.Value, v reflect.Value) bool) error { omv := reflect.ValueOf(orderedMap) // First get the ordered keys, and then index into each of the values associated with it. diff --git a/ygot/render_test.go b/ygot/render_test.go index e0255188f..df8b60fc9 100644 --- a/ygot/render_test.go +++ b/ygot/render_test.go @@ -2049,6 +2049,14 @@ func (o *MultiKeyOrderedMap) Values() []*OrderedMultikeyedList { return values } +// Len returns a size of OrderedMultikeyedList_OrderedMap +func (o *MultiKeyOrderedMap) Len() int { + if o == nil { + return 0 + } + return len(o.keys) +} + // AppendNew creates and appends a new OrderedMultikeyedList, returning the // newly-initialized v. It returns an error if the v already exists. func (o *MultiKeyOrderedMap) AppendNew(Key1 string, Key2 uint64) (*OrderedMultikeyedList, error) { diff --git a/ygot/struct_validation_map_test.go b/ygot/struct_validation_map_test.go index 93ba4430a..03c986dec 100644 --- a/ygot/struct_validation_map_test.go +++ b/ygot/struct_validation_map_test.go @@ -514,6 +514,14 @@ func (o *OrderedMap) Values() []*OrderedList { return values } +// Len returns a size of OrderedList_OrderedMap +func (o *OrderedMap) Len() int { + if o == nil { + return 0 + } + return len(o.keys) +} + // Get returns the value corresponding to the key. If the key is not found, nil // is returned. func (o *OrderedMap) Get(key string) *OrderedList { diff --git a/ygot/types.go b/ygot/types.go index 84b7855d3..de2283408 100644 --- a/ygot/types.go +++ b/ygot/types.go @@ -96,6 +96,8 @@ type GoOrderedList interface { // IsYANGOrderedList is a marker method that indicates that the struct // implements the GoOrderedList interface. IsYANGOrderedList() + // Len returns the size of the ordered list. + Len() int } // KeyHelperGoStruct is an interface which can be implemented by Go structs diff --git a/ytypes/list.go b/ytypes/list.go index 99d7a7a38..ae43a530a 100644 --- a/ytypes/list.go +++ b/ytypes/list.go @@ -44,40 +44,49 @@ func validateList(schema *yang.Entry, value interface{}) util.Errors { util.DbgPrint("validateList with value %v, type %T, schema name %s", value, value, schema.Name) kind := reflect.TypeOf(value).Kind() - if kind == reflect.Slice || kind == reflect.Map { + orderedMap, isOrderedMap := value.(ygot.GoOrderedList) + if kind == reflect.Slice || kind == reflect.Map || isOrderedMap { // Check list attributes: size constraints etc. // Skip this check if not a list type - in this case value may be a list // element which shares the list schema (excluding ListAttr). errors = util.AppendErrs(errors, validateListAttr(schema, value)) } - switch kind { - case reflect.Slice: + checkMapElement := func(key, val reflect.Value) { + structElems := val.Elem() + // Check that keys are present and have correct values. + errors = util.AppendErrs(errors, checkKeys(schema, structElems, key)) + + // Verify each elements's fields. + errors = util.AppendErrs(errors, validateStructElems(schema, val.Interface())) + } + + switch { + case isOrderedMap: + errors = util.AppendErr(errors, yreflect.RangeOrderedMap(orderedMap, func(k, v reflect.Value) bool { + checkMapElement(k, v) + return true + })) + case kind == reflect.Slice: // List without key is a slice in the data tree. sv := reflect.ValueOf(value) for i := 0; i < sv.Len(); i++ { errors = util.AppendErrs(errors, validateStructElems(schema, sv.Index(i).Interface())) } - case reflect.Map: + case kind == reflect.Map: // List with key is a map in the data tree, with the key being the value // of the key field(s) in the elements. for _, key := range reflect.ValueOf(value).MapKeys() { - cv := reflect.ValueOf(value).MapIndex(key).Interface() - structElems := reflect.ValueOf(cv).Elem() - // Check that keys are present and have correct values. - errors = util.AppendErrs(errors, checkKeys(schema, structElems, key)) - - // Verify each elements's fields. - errors = util.AppendErrs(errors, validateStructElems(schema, cv)) + checkMapElement(key, reflect.ValueOf(value).MapIndex(key)) } - case reflect.Ptr: + case kind == reflect.Ptr: // Validate was called on a list element rather than the whole list, or // on a completely bogus struct. In either case, evaluate just the // element against the list schema without considering list attributes. errors = util.AppendErrs(errors, validateStructElems(schema, value)) default: - errors = util.AppendErr(errors, fmt.Errorf("validateList expected map/slice type for %s, got %T", schema.Name, value)) + errors = util.AppendErr(errors, fmt.Errorf("validateList expected map/slice/GoOrderedList type for %s, got %T", schema.Name, value)) } return errors diff --git a/ytypes/list_exported_test.go b/ytypes/list_exported_test.go index 183786369..bcd2a6b6b 100644 --- a/ytypes/list_exported_test.go +++ b/ytypes/list_exported_test.go @@ -354,3 +354,86 @@ func TestUnmarshalSingleListElement(t *testing.T) { }) } } + +func TestValidatedOrderedMap(t *testing.T) { + tests := []struct { + desc string + inSchema *yang.Entry + inVal any + wantErr bool + }{{ + desc: "single-keyed list", + inSchema: ctestschema.SchemaTree["OrderedList"], + inVal: ctestschema.GetOrderedMap(t), + }, { + desc: "multi-keyed list", + inSchema: ctestschema.SchemaTree["OrderedMultikeyedList"], + inVal: ctestschema.GetOrderedMapMultikeyed(t), + }, { + desc: "single-keyed list with missing key", + inSchema: ctestschema.SchemaTree["OrderedList"], + inVal: func() *ctestschema.OrderedList_OrderedMap { + om := ctestschema.GetOrderedMap(t) + om.Get("foo").Key = nil + return om + }(), + wantErr: true, + }, { + desc: "single-keyed list with mismatching key", + inSchema: ctestschema.SchemaTree["OrderedList"], + inVal: func() *ctestschema.OrderedList_OrderedMap { + om := ctestschema.GetOrderedMap(t) + om.Get("foo").Key = ygot.String("foosball") + return om + }(), + wantErr: true, + }, { + desc: "single-keyed list with too many elements", + inSchema: ctestschema.SchemaTree["OrderedList"], + inVal: func() *ctestschema.OrderedList_OrderedMap { + om := &ctestschema.OrderedList_OrderedMap{} + om.AppendNew("alpha") + om.AppendNew("bravo") + om.AppendNew("charlie") + om.AppendNew("delta") + om.AppendNew("echo") + // One too many + om.AppendNew("foxtrot") + return om + }(), + wantErr: true, + }, { + desc: "multi-keyed list with missing key", + inSchema: ctestschema.SchemaTree["OrderedMultikeyedList"], + inVal: func() *ctestschema.OrderedMultikeyedList_OrderedMap { + om := ctestschema.GetOrderedMapMultikeyed(t) + om.Get(ctestschema.OrderedMultikeyedList_Key{ + Key1: "foo", + Key2: 42, + }).Key2 = nil + return om + }, + wantErr: true, + }, { + desc: "multi-keyed list with mismatching key", + inSchema: ctestschema.SchemaTree["OrderedMultikeyedList"], + inVal: func() *ctestschema.OrderedMultikeyedList_OrderedMap { + om := ctestschema.GetOrderedMapMultikeyed(t) + om.Get(ctestschema.OrderedMultikeyedList_Key{ + Key1: "foo", + Key2: 42, + }).Key2 = ygot.Uint64(43) + return om + }, + wantErr: true, + }} + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + errs := ytypes.Validate(tt.inSchema, tt.inVal) + if got, want := (errs != nil), tt.wantErr; got != want { + t.Errorf("%s: b.Validate(%v) got error: %v, want error? %v", tt.desc, tt.inVal, errs, tt.wantErr) + } + }) + } +} diff --git a/ytypes/list_test.go b/ytypes/list_test.go index 5addec371..40b2c1c6a 100644 --- a/ytypes/list_test.go +++ b/ytypes/list_test.go @@ -136,7 +136,7 @@ func TestValidateList(t *testing.T) { // bad value type err = util.Errors(validateList(validListSchema, struct{}{})).Error() - wantErr = `validateList expected map/slice type for valid-list-schema, got struct {}` + wantErr = `validateList expected map/slice/GoOrderedList type for valid-list-schema, got struct {}` if got, want := err, wantErr; got != want { t.Errorf("nil schema: Unmarshal got error: %v, want error: %v", got, want) } diff --git a/ytypes/util_schema.go b/ytypes/util_schema.go index 2cb4c555f..c33409d76 100644 --- a/ytypes/util_schema.go +++ b/ytypes/util_schema.go @@ -22,6 +22,7 @@ import ( "github.com/openconfig/goyang/pkg/yang" "github.com/openconfig/ygot/util" + "github.com/openconfig/ygot/ygot" ) //lint:file-ignore U1000 Ignore all unused code, it represents generated code. @@ -91,8 +92,12 @@ func validateListAttr(schema *yang.Entry, value interface{}) util.Errors { var size uint64 if value != nil { - switch reflect.TypeOf(value).Kind() { - case reflect.Slice, reflect.Map: + orderedMap, isOrderedMap := value.(ygot.GoOrderedList) + kind := reflect.TypeOf(value).Kind() + switch { + case isOrderedMap: + size = uint64(orderedMap.Len()) + case kind == reflect.Slice, kind == reflect.Map: size = uint64(reflect.ValueOf(value).Len()) default: return util.NewErrs(fmt.Errorf("value %v type %T must be map or slice type for schema %s", value, value, schema.Name)) diff --git a/ytypes/validate_test.go b/ytypes/validate_test.go index 2f64b9c6b..edabbeea9 100644 --- a/ytypes/validate_test.go +++ b/ytypes/validate_test.go @@ -70,6 +70,7 @@ func customValidation(val ygot.GoStruct) error { } return nil } + func TestValidate(t *testing.T) { leafSchema := &yang.Entry{Name: "leaf-schema", Kind: yang.LeafEntry, Type: &yang.YangType{Kind: yang.Ystring}}