From ad5633924658bbe4dec38d1532939b93777c3c06 Mon Sep 17 00:00:00 2001 From: Wen Bo Li <50884368+wenovus@users.noreply.github.com> Date: Wed, 4 Oct 2023 20:10:52 -0400 Subject: [PATCH] Add missing nil check for GetOrderedMap accessors (#918) --- gogen/gogen_test.go | 3 +++ gogen/ordered_list.go | 3 +++ .../openconfig-withlist-opstate.formatted-txt | 3 +++ .../structs/openconfig-withlist.formatted-txt | 3 +++ .../schemaops/ctestschema/ctestschema.go | 15 ++++++++++++--- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/gogen/gogen_test.go b/gogen/gogen_test.go index 78e03cdf..2da31312 100644 --- a/gogen/gogen_test.go +++ b/gogen/gogen_test.go @@ -877,6 +877,9 @@ func (s *Tstruct) AppendListWithKey(v *Tstruct_ListWithKey) error { // is nil, or the specified key is not present in the list, nil is returned // such that Get* methods may be safely chained. func (s *Tstruct) GetListWithKey(KeyLeaf string) *Tstruct_ListWithKey { + if s == nil { + return nil + } key := KeyLeaf return s.ListWithKey.Get(key) } diff --git a/gogen/ordered_list.go b/gogen/ordered_list.go index a1b11c5e..8141fc09 100644 --- a/gogen/ordered_list.go +++ b/gogen/ordered_list.go @@ -94,6 +94,9 @@ func (s *{{ .ParentStructName }}) Get{{ .ListFieldName }}( {{- if ne (inc $i) $length -}}, {{ end -}} {{- end -}} ) *{{ .ListTypeName }} { + if s == nil { + return nil + } {{ if gt (len .Keys) 1 -}} key := {{ .KeyName }}{ {{- range $key := .Keys }} diff --git a/gogen/testdata/structs/openconfig-withlist-opstate.formatted-txt b/gogen/testdata/structs/openconfig-withlist-opstate.formatted-txt index 523ac0f1..77fc6b7e 100644 --- a/gogen/testdata/structs/openconfig-withlist-opstate.formatted-txt +++ b/gogen/testdata/structs/openconfig-withlist-opstate.formatted-txt @@ -223,6 +223,9 @@ func (s *Model) AppendSingleKeyOrdered(v *Model_SingleKeyOrdered) error { // is nil, or the specified key is not present in the list, nil is returned // such that Get* methods may be safely chained. func (s *Model) GetSingleKeyOrdered(Key string) *Model_SingleKeyOrdered { + if s == nil { + return nil + } key := Key return s.SingleKeyOrdered.Get(key) } diff --git a/gogen/testdata/structs/openconfig-withlist.formatted-txt b/gogen/testdata/structs/openconfig-withlist.formatted-txt index 1b47f9c5..248c0782 100644 --- a/gogen/testdata/structs/openconfig-withlist.formatted-txt +++ b/gogen/testdata/structs/openconfig-withlist.formatted-txt @@ -223,6 +223,9 @@ func (s *Model) AppendSingleKeyOrdered(v *Model_SingleKeyOrdered) error { // is nil, or the specified key is not present in the list, nil is returned // such that Get* methods may be safely chained. func (s *Model) GetSingleKeyOrdered(Key string) *Model_SingleKeyOrdered { + if s == nil { + return nil + } key := Key return s.SingleKeyOrdered.Get(key) } diff --git a/integration_tests/schemaops/ctestschema/ctestschema.go b/integration_tests/schemaops/ctestschema/ctestschema.go index 3d68c402..6e01315d 100644 --- a/integration_tests/schemaops/ctestschema/ctestschema.go +++ b/integration_tests/schemaops/ctestschema/ctestschema.go @@ -322,6 +322,9 @@ func (s *Device) AppendOrderedList(v *OrderedList) error { // is nil, or the specified key is not present in the list, nil is returned // such that Get* methods may be safely chained. func (s *Device) GetOrderedList(Key string) *OrderedList { + if s == nil { + return nil + } key := Key return s.OrderedList.Get(key) } @@ -342,7 +345,7 @@ type OrderedList_OrderedMap struct { } // IsYANGOrderedList ensures that OrderedList_OrderedMap implements the -// ygot.GoOrderedList interface. +// ygot.GoOrderedMap interface. func (*OrderedList_OrderedMap) IsYANGOrderedList() {} // init initializes any uninitialized values. @@ -481,6 +484,9 @@ func (s *Device) AppendOrderedMultikeyedList(v *OrderedMultikeyedList) error { // is nil, or the specified key is not present in the list, nil is returned // such that Get* methods may be safely chained. func (s *Device) GetOrderedMultikeyedList(Key1 string, Key2 uint64) *OrderedMultikeyedList { + if s == nil { + return nil + } key := OrderedMultikeyedList_Key{ Key1: Key1, Key2: Key2, @@ -507,7 +513,7 @@ type OrderedMultikeyedList_OrderedMap struct { } // IsYANGOrderedList ensures that OrderedMultikeyedList_OrderedMap implements the -// ygot.GoOrderedList interface. +// ygot.GoOrderedMap interface. func (*OrderedMultikeyedList_OrderedMap) IsYANGOrderedList() {} // init initializes any uninitialized values. @@ -782,6 +788,9 @@ func (s *OrderedList) AppendOrderedList(v *OrderedList_OrderedList) error { // is nil, or the specified key is not present in the list, nil is returned // such that Get* methods may be safely chained. func (s *OrderedList) GetOrderedList(Key string) *OrderedList_OrderedList { + if s == nil { + return nil + } key := Key return s.OrderedList.Get(key) } @@ -802,7 +811,7 @@ type OrderedList_OrderedList_OrderedMap struct { } // IsYANGOrderedList ensures that OrderedList_OrderedList_OrderedMap implements the -// ygot.GoOrderedList interface. +// ygot.GoOrderedMap interface. func (*OrderedList_OrderedList_OrderedMap) IsYANGOrderedList() {} // init initializes any uninitialized values.