Skip to content

Commit c52408e

Browse files
authored
Add GetOrCreate<ListName>Map helper for lists. (#971)
This helps avoid nil checks when there is a need to operate on the map field directly.
1 parent 9cb751b commit c52408e

File tree

15 files changed

+169437
-129862
lines changed

15 files changed

+169437
-129862
lines changed

exampleoc/oc.go

Lines changed: 54696 additions & 44967 deletions
Large diffs are not rendered by default.

exampleoc/ocpath.go

Lines changed: 6774 additions & 86 deletions
Large diffs are not rendered by default.

exampleoc/opstateoc/oc.go

Lines changed: 54023 additions & 44703 deletions
Large diffs are not rendered by default.

gogen/gogen_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,17 @@ type Tstruct struct {
851851
func (*Tstruct) IsYANGGoStruct() {}
852852
`,
853853
methods: `
854+
// GetOrCreateListWithKeyMap returns the ordered map field
855+
// ListWithKey from Tstruct.
856+
//
857+
// It initializes the field if not already initialized.
858+
func (s *Tstruct) GetOrCreateListWithKeyMap() *Tstruct_ListWithKey_OrderedMap {
859+
if s.ListWithKey == nil {
860+
s.ListWithKey = &Tstruct_ListWithKey_OrderedMap{}
861+
}
862+
return s.ListWithKey
863+
}
864+
854865
// AppendNewListWithKey creates a new entry in the ListWithKey
855866
// ordered map of the Tstruct struct. The keys of the list are
856867
// populated from the input arguments.
@@ -1548,6 +1559,16 @@ func (t *Tstruct) NewListWithKey(KeyLeafOne string, KeyLeafTwo int8) (*Tstruct_L
15481559
return t.ListWithKey[key], nil
15491560
}
15501561
1562+
// GetOrCreateListWithKeyMap returns the list (map) from Tstruct.
1563+
//
1564+
// It initializes the field if not already initialized.
1565+
func (t *Tstruct) GetOrCreateListWithKeyMap() map[Tstruct_ListWithKey_Key]*Tstruct_ListWithKey {
1566+
if t.ListWithKey == nil {
1567+
t.ListWithKey = make(map[Tstruct_ListWithKey_Key]*Tstruct_ListWithKey)
1568+
}
1569+
return t.ListWithKey
1570+
}
1571+
15511572
// GetOrCreateListWithKey retrieves the value with the specified keys from
15521573
// the receiver Tstruct. If the entry does not exist, then it is created.
15531574
// It returns the existing or new list member.
@@ -1768,6 +1789,16 @@ func (t *Tstruct) NewListWithKey(KeyLeaf string) (*Tstruct_ListWithKey, error){
17681789
return t.ListWithKey[key], nil
17691790
}
17701791
1792+
// GetOrCreateListWithKeyMap returns the list (map) from Tstruct.
1793+
//
1794+
// It initializes the field if not already initialized.
1795+
func (t *Tstruct) GetOrCreateListWithKeyMap() map[string]*Tstruct_ListWithKey {
1796+
if t.ListWithKey == nil {
1797+
t.ListWithKey = make(map[string]*Tstruct_ListWithKey)
1798+
}
1799+
return t.ListWithKey
1800+
}
1801+
17711802
// GetOrCreateListWithKey retrieves the value with the specified keys from
17721803
// the receiver Tstruct. If the entry does not exist, then it is created.
17731804
// It returns the existing or new list member.

gogen/internal/gotypes/ordered_map.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ type RoutingPolicy_PolicyDefinition struct {
3333
Statement *RoutingPolicy_PolicyDefinition_Statement_OrderedMap
3434
}
3535

36+
// GetOrCreateStatementMap returns the ordered map field
37+
// Statement from RoutingPolicy_PolicyDefinition.
38+
//
39+
// It initializes the field if not already initialized.
40+
func (s *RoutingPolicy_PolicyDefinition) GetOrCreateStatementMap() *RoutingPolicy_PolicyDefinition_Statement_OrderedMap {
41+
if s.Statement == nil {
42+
s.Statement = &RoutingPolicy_PolicyDefinition_Statement_OrderedMap{}
43+
}
44+
return s.Statement
45+
}
46+
3647
// AppendNewStatement appends a new policy statement with the given key.
3748
func (s *RoutingPolicy_PolicyDefinition) AppendNewStatement(key string) (*RoutingPolicy_PolicyDefinition_Statement, error) {
3849
if s.Statement == nil {

gogen/internal/gotypes/ordered_map_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,12 @@ func TestOrderedMapFromParent(t *testing.T) {
240240
},
241241
})
242242
}
243+
244+
func TestOrderedMapFromParentGetOrCreate(t *testing.T) {
245+
p := &RoutingPolicy_PolicyDefinition{}
246+
247+
p.GetOrCreateStatementMap()
248+
if p.Statement == nil {
249+
t.Errorf("Statement must be initialized")
250+
}
251+
}

gogen/ordered_list.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ func OrderedMapTypeName(listElemTypeName string) string {
5050

5151
var (
5252
goOrderedMapParentMethodsTemplate = mustMakeTemplate("orderedMapParentMethods", `
53+
// GetOrCreate{{ .ListFieldName }}Map returns the ordered map field
54+
// {{ .ListFieldName }} from {{ .ParentStructName }}.
55+
//
56+
// It initializes the field if not already initialized.
57+
func (s *{{ .ParentStructName }}) GetOrCreate{{ .ListFieldName }}Map() *{{ .StructName }} {
58+
if s.{{ .ListFieldName }} == nil {
59+
s.{{ .ListFieldName }} = &{{ .StructName }}{}
60+
}
61+
return s.{{ .ListFieldName }}
62+
}
63+
5364
// AppendNew{{ .ListFieldName }} creates a new entry in the {{ .ListFieldName }}
5465
// ordered map of the {{ .ParentStructName }} struct. The keys of the list are
5566
// populated from the input arguments.

gogen/testdata/structs/openconfig-list-enum-key.getters-append.formatted-txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ func (t *OpenconfigListEnumKey_Top_MultiKey) NewEkm(K1 E_OpenconfigListEnumKey_T
222222
return t.Ekm[key], nil
223223
}
224224

225+
// GetOrCreateEkmMap returns the list (map) from OpenconfigListEnumKey_Top_MultiKey.
226+
//
227+
// It initializes the field if not already initialized.
228+
func (t *OpenconfigListEnumKey_Top_MultiKey) GetOrCreateEkmMap() map[OpenconfigListEnumKey_Top_MultiKey_Ekm_Key]*OpenconfigListEnumKey_Top_MultiKey_Ekm {
229+
if t.Ekm == nil {
230+
t.Ekm = make(map[OpenconfigListEnumKey_Top_MultiKey_Ekm_Key]*OpenconfigListEnumKey_Top_MultiKey_Ekm)
231+
}
232+
return t.Ekm
233+
}
234+
225235
// GetOrCreateEkm retrieves the value with the specified keys from
226236
// the receiver OpenconfigListEnumKey_Top_MultiKey. If the entry does not exist, then it is created.
227237
// It returns the existing or new list member.
@@ -509,6 +519,16 @@ func (t *OpenconfigListEnumKey_Top_SingleKey) NewEks(K E_OpenconfigListEnumKey_T
509519
return t.Eks[key], nil
510520
}
511521

522+
// GetOrCreateEksMap returns the list (map) from OpenconfigListEnumKey_Top_SingleKey.
523+
//
524+
// It initializes the field if not already initialized.
525+
func (t *OpenconfigListEnumKey_Top_SingleKey) GetOrCreateEksMap() map[E_OpenconfigListEnumKey_Top_SingleKey_Eks_Config_K]*OpenconfigListEnumKey_Top_SingleKey_Eks {
526+
if t.Eks == nil {
527+
t.Eks = make(map[E_OpenconfigListEnumKey_Top_SingleKey_Eks_Config_K]*OpenconfigListEnumKey_Top_SingleKey_Eks)
528+
}
529+
return t.Eks
530+
}
531+
512532
// GetOrCreateEks retrieves the value with the specified keys from
513533
// the receiver OpenconfigListEnumKey_Top_SingleKey. If the entry does not exist, then it is created.
514534
// It returns the existing or new list member.

gogen/testdata/structs/openconfig-withlist-opstate.formatted-txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ func (t *Model) RenameSingleKey(oldK, newK string) error {
197197
return nil
198198
}
199199

200+
// GetOrCreateSingleKeyOrderedMap returns the ordered map field
201+
// SingleKeyOrdered from Model.
202+
//
203+
// It initializes the field if not already initialized.
204+
func (s *Model) GetOrCreateSingleKeyOrderedMap() *Model_SingleKeyOrdered_OrderedMap {
205+
if s.SingleKeyOrdered == nil {
206+
s.SingleKeyOrdered = &Model_SingleKeyOrdered_OrderedMap{}
207+
}
208+
return s.SingleKeyOrdered
209+
}
210+
200211
// AppendNewSingleKeyOrdered creates a new entry in the SingleKeyOrdered
201212
// ordered map of the Model struct. The keys of the list are
202213
// populated from the input arguments.

gogen/testdata/structs/openconfig-withlist.formatted-txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ func (t *Model) RenameSingleKey(oldK, newK string) error {
197197
return nil
198198
}
199199

200+
// GetOrCreateSingleKeyOrderedMap returns the ordered map field
201+
// SingleKeyOrdered from Model.
202+
//
203+
// It initializes the field if not already initialized.
204+
func (s *Model) GetOrCreateSingleKeyOrderedMap() *Model_SingleKeyOrdered_OrderedMap {
205+
if s.SingleKeyOrdered == nil {
206+
s.SingleKeyOrdered = &Model_SingleKeyOrdered_OrderedMap{}
207+
}
208+
return s.SingleKeyOrdered
209+
}
210+
200211
// AppendNewSingleKeyOrdered creates a new entry in the SingleKeyOrdered
201212
// ordered map of the Model struct. The keys of the list are
202213
// populated from the input arguments.

gogen/unordered_list.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ func (t *{{ .Receiver }}) Get{{ .ListName }}(
198198
}
199199
`)
200200

201-
// goGetOrCreateListTemplate defines a template for a function that, for a
201+
// goGetOrCreateListElementTemplate defines a template for a function that, for a
202202
// particular list key, gets an existing map value, or creates it if it doesn't
203203
// exist.
204-
goGetOrCreateListTemplate = mustMakeTemplate("getOrCreateList", `
204+
goGetOrCreateListElementTemplate = mustMakeTemplate("getOrCreateListElement", `
205205
// GetOrCreate{{ .ListName }} retrieves the value with the specified keys from
206206
// the receiver {{ .Receiver }}. If the entry does not exist, then it is created.
207207
// It returns the existing or new list member.
@@ -240,6 +240,36 @@ func (t *{{ .Receiver }}) GetOrCreate{{ .ListName }}(
240240
}
241241
return v
242242
}
243+
`)
244+
245+
// goGetOrCreateListTemplate defines a template for a function that
246+
// returns the current list. It also creates it if it doesn't exist.
247+
goGetOrCreateListTemplate = mustMakeTemplate("getOrCreateList", `
248+
// GetOrCreate{{ .ListName }}Map returns the list (map) from {{ .Receiver }}.
249+
//
250+
// It initializes the field if not already initialized.
251+
func (t *{{ .Receiver }}) GetOrCreate{{ .ListName }}Map() {{ if ne .KeyStruct "" -}}
252+
map[{{ .KeyStruct }}]*{{ .ListType }}
253+
{{- else }}
254+
{{- $listName := .ListName -}}
255+
{{- $listType := .ListType -}}
256+
{{- range $key := .Keys -}}
257+
map[{{ $key.Type }}]*{{ $listType }}
258+
{{- end }}
259+
{{- end }} {
260+
if t.{{ .ListName }} == nil {
261+
{{- if ne .KeyStruct "" }}
262+
t.{{ .ListName }} = make(map[{{ .KeyStruct }}]*{{ .ListType }})
263+
{{- else }}
264+
{{- $listName := .ListName -}}
265+
{{- $listType := .ListType -}}
266+
{{- range $key := .Keys }}
267+
t.{{ $listName }} = make(map[{{ $key.Type }}]*{{ $listType }})
268+
{{- end }}
269+
{{- end }}
270+
}
271+
return t.{{ .ListName }}
272+
}
243273
`)
244274

245275
// goDeleteListTemplate defines a template for a function that, for a
@@ -423,7 +453,10 @@ func (t *{{ .Receiver }}) ΛListKeyMap() (map[string]interface{}, error) {
423453
// The generated function is written to the supplied buffer, using the method
424454
// argument to determine the list's characteristics in the template.
425455
func generateGetOrCreateList(buf *bytes.Buffer, method *generatedGoListMethod) error {
426-
return goGetOrCreateListTemplate.Execute(buf, method)
456+
if err := goGetOrCreateListTemplate.Execute(buf, method); err != nil {
457+
return err
458+
}
459+
return goGetOrCreateListElementTemplate.Execute(buf, method)
427460
}
428461

429462
// generateListGetter generates a getter function for members of the a YANG list

integration_tests/schemaops/ctestschema/ctestschema.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ func (t *Device) RenameUnorderedList(oldK, newK string) error {
214214
return nil
215215
}
216216

217+
// GetOrCreateUnorderedListMap returns the list (map) from Device.
218+
//
219+
// It initializes the field if not already initialized.
220+
func (t *Device) GetOrCreateUnorderedListMap() map[string]*UnorderedList {
221+
if t.UnorderedList == nil {
222+
t.UnorderedList = make(map[string]*UnorderedList)
223+
}
224+
return t.UnorderedList
225+
}
226+
217227
// GetOrCreateUnorderedList retrieves the value with the specified keys from
218228
// the receiver Device. If the entry does not exist, then it is created.
219229
// It returns the existing or new list member.
@@ -296,6 +306,17 @@ func (t *Device) GetOtherData() *OtherData {
296306
return nil
297307
}
298308

309+
// GetOrCreateOrderedListMap returns the ordered map field
310+
// OrderedList from Device.
311+
//
312+
// It initializes the field if not already initialized.
313+
func (s *Device) GetOrCreateOrderedListMap() *OrderedList_OrderedMap {
314+
if s.OrderedList == nil {
315+
s.OrderedList = &OrderedList_OrderedMap{}
316+
}
317+
return s.OrderedList
318+
}
319+
299320
// AppendNewOrderedList creates a new entry in the OrderedList
300321
// ordered map of the Device struct. The keys of the list are
301322
// populated from the input arguments.
@@ -458,6 +479,17 @@ func (o *OrderedList_OrderedMap) AppendNew(Key string) (*OrderedList, error) {
458479
return newElement, nil
459480
}
460481

482+
// GetOrCreateOrderedMultikeyedListMap returns the ordered map field
483+
// OrderedMultikeyedList from Device.
484+
//
485+
// It initializes the field if not already initialized.
486+
func (s *Device) GetOrCreateOrderedMultikeyedListMap() *OrderedMultikeyedList_OrderedMap {
487+
if s.OrderedMultikeyedList == nil {
488+
s.OrderedMultikeyedList = &OrderedMultikeyedList_OrderedMap{}
489+
}
490+
return s.OrderedMultikeyedList
491+
}
492+
461493
// AppendNewOrderedMultikeyedList creates a new entry in the OrderedMultikeyedList
462494
// ordered map of the Device struct. The keys of the list are
463495
// populated from the input arguments.
@@ -762,6 +794,17 @@ func (t *OrderedList) GetValue() string {
762794
return *t.Value
763795
}
764796

797+
// GetOrCreateOrderedListMap returns the ordered map field
798+
// OrderedList from OrderedList.
799+
//
800+
// It initializes the field if not already initialized.
801+
func (s *OrderedList) GetOrCreateOrderedListMap() *OrderedList_OrderedList_OrderedMap {
802+
if s.OrderedList == nil {
803+
s.OrderedList = &OrderedList_OrderedList_OrderedMap{}
804+
}
805+
return s.OrderedList
806+
}
807+
765808
// AppendNewOrderedList creates a new entry in the OrderedList
766809
// ordered map of the OrderedList struct. The keys of the list are
767810
// populated from the input arguments.

0 commit comments

Comments
 (0)