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.

0 commit comments

Comments
 (0)