Skip to content

Commit

Permalink
Add GetOrCreate<ListName>Map helper for lists. (#971)
Browse files Browse the repository at this point in the history
This helps avoid nil checks when there is a need to operate on the map
field directly.
  • Loading branch information
wenovus authored Apr 25, 2024
1 parent 9cb751b commit c52408e
Show file tree
Hide file tree
Showing 15 changed files with 169,437 additions and 129,862 deletions.
99,663 changes: 54,696 additions & 44,967 deletions exampleoc/oc.go

Large diffs are not rendered by default.

6,860 changes: 6,774 additions & 86 deletions exampleoc/ocpath.go

Large diffs are not rendered by default.

98,726 changes: 54,023 additions & 44,703 deletions exampleoc/opstateoc/oc.go

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions gogen/gogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,17 @@ type Tstruct struct {
func (*Tstruct) IsYANGGoStruct() {}
`,
methods: `
// GetOrCreateListWithKeyMap returns the ordered map field
// ListWithKey from Tstruct.
//
// It initializes the field if not already initialized.
func (s *Tstruct) GetOrCreateListWithKeyMap() *Tstruct_ListWithKey_OrderedMap {
if s.ListWithKey == nil {
s.ListWithKey = &Tstruct_ListWithKey_OrderedMap{}
}
return s.ListWithKey
}
// AppendNewListWithKey creates a new entry in the ListWithKey
// ordered map of the Tstruct struct. The keys of the list are
// populated from the input arguments.
Expand Down Expand Up @@ -1548,6 +1559,16 @@ func (t *Tstruct) NewListWithKey(KeyLeafOne string, KeyLeafTwo int8) (*Tstruct_L
return t.ListWithKey[key], nil
}
// GetOrCreateListWithKeyMap returns the list (map) from Tstruct.
//
// It initializes the field if not already initialized.
func (t *Tstruct) GetOrCreateListWithKeyMap() map[Tstruct_ListWithKey_Key]*Tstruct_ListWithKey {
if t.ListWithKey == nil {
t.ListWithKey = make(map[Tstruct_ListWithKey_Key]*Tstruct_ListWithKey)
}
return t.ListWithKey
}
// GetOrCreateListWithKey retrieves the value with the specified keys from
// the receiver Tstruct. If the entry does not exist, then it is created.
// It returns the existing or new list member.
Expand Down Expand Up @@ -1768,6 +1789,16 @@ func (t *Tstruct) NewListWithKey(KeyLeaf string) (*Tstruct_ListWithKey, error){
return t.ListWithKey[key], nil
}
// GetOrCreateListWithKeyMap returns the list (map) from Tstruct.
//
// It initializes the field if not already initialized.
func (t *Tstruct) GetOrCreateListWithKeyMap() map[string]*Tstruct_ListWithKey {
if t.ListWithKey == nil {
t.ListWithKey = make(map[string]*Tstruct_ListWithKey)
}
return t.ListWithKey
}
// GetOrCreateListWithKey retrieves the value with the specified keys from
// the receiver Tstruct. If the entry does not exist, then it is created.
// It returns the existing or new list member.
Expand Down
11 changes: 11 additions & 0 deletions gogen/internal/gotypes/ordered_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ type RoutingPolicy_PolicyDefinition struct {
Statement *RoutingPolicy_PolicyDefinition_Statement_OrderedMap
}

// GetOrCreateStatementMap returns the ordered map field
// Statement from RoutingPolicy_PolicyDefinition.
//
// It initializes the field if not already initialized.
func (s *RoutingPolicy_PolicyDefinition) GetOrCreateStatementMap() *RoutingPolicy_PolicyDefinition_Statement_OrderedMap {
if s.Statement == nil {
s.Statement = &RoutingPolicy_PolicyDefinition_Statement_OrderedMap{}
}
return s.Statement
}

// AppendNewStatement appends a new policy statement with the given key.
func (s *RoutingPolicy_PolicyDefinition) AppendNewStatement(key string) (*RoutingPolicy_PolicyDefinition_Statement, error) {
if s.Statement == nil {
Expand Down
9 changes: 9 additions & 0 deletions gogen/internal/gotypes/ordered_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,12 @@ func TestOrderedMapFromParent(t *testing.T) {
},
})
}

func TestOrderedMapFromParentGetOrCreate(t *testing.T) {
p := &RoutingPolicy_PolicyDefinition{}

p.GetOrCreateStatementMap()
if p.Statement == nil {
t.Errorf("Statement must be initialized")
}
}
11 changes: 11 additions & 0 deletions gogen/ordered_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func OrderedMapTypeName(listElemTypeName string) string {

var (
goOrderedMapParentMethodsTemplate = mustMakeTemplate("orderedMapParentMethods", `
// GetOrCreate{{ .ListFieldName }}Map returns the ordered map field
// {{ .ListFieldName }} from {{ .ParentStructName }}.
//
// It initializes the field if not already initialized.
func (s *{{ .ParentStructName }}) GetOrCreate{{ .ListFieldName }}Map() *{{ .StructName }} {
if s.{{ .ListFieldName }} == nil {
s.{{ .ListFieldName }} = &{{ .StructName }}{}
}
return s.{{ .ListFieldName }}
}
// AppendNew{{ .ListFieldName }} creates a new entry in the {{ .ListFieldName }}
// ordered map of the {{ .ParentStructName }} struct. The keys of the list are
// populated from the input arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ func (t *OpenconfigListEnumKey_Top_MultiKey) NewEkm(K1 E_OpenconfigListEnumKey_T
return t.Ekm[key], nil
}

// GetOrCreateEkmMap returns the list (map) from OpenconfigListEnumKey_Top_MultiKey.
//
// It initializes the field if not already initialized.
func (t *OpenconfigListEnumKey_Top_MultiKey) GetOrCreateEkmMap() map[OpenconfigListEnumKey_Top_MultiKey_Ekm_Key]*OpenconfigListEnumKey_Top_MultiKey_Ekm {
if t.Ekm == nil {
t.Ekm = make(map[OpenconfigListEnumKey_Top_MultiKey_Ekm_Key]*OpenconfigListEnumKey_Top_MultiKey_Ekm)
}
return t.Ekm
}

// GetOrCreateEkm retrieves the value with the specified keys from
// the receiver OpenconfigListEnumKey_Top_MultiKey. If the entry does not exist, then it is created.
// It returns the existing or new list member.
Expand Down Expand Up @@ -509,6 +519,16 @@ func (t *OpenconfigListEnumKey_Top_SingleKey) NewEks(K E_OpenconfigListEnumKey_T
return t.Eks[key], nil
}

// GetOrCreateEksMap returns the list (map) from OpenconfigListEnumKey_Top_SingleKey.
//
// It initializes the field if not already initialized.
func (t *OpenconfigListEnumKey_Top_SingleKey) GetOrCreateEksMap() map[E_OpenconfigListEnumKey_Top_SingleKey_Eks_Config_K]*OpenconfigListEnumKey_Top_SingleKey_Eks {
if t.Eks == nil {
t.Eks = make(map[E_OpenconfigListEnumKey_Top_SingleKey_Eks_Config_K]*OpenconfigListEnumKey_Top_SingleKey_Eks)
}
return t.Eks
}

// GetOrCreateEks retrieves the value with the specified keys from
// the receiver OpenconfigListEnumKey_Top_SingleKey. If the entry does not exist, then it is created.
// It returns the existing or new list member.
Expand Down
11 changes: 11 additions & 0 deletions gogen/testdata/structs/openconfig-withlist-opstate.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ func (t *Model) RenameSingleKey(oldK, newK string) error {
return nil
}

// GetOrCreateSingleKeyOrderedMap returns the ordered map field
// SingleKeyOrdered from Model.
//
// It initializes the field if not already initialized.
func (s *Model) GetOrCreateSingleKeyOrderedMap() *Model_SingleKeyOrdered_OrderedMap {
if s.SingleKeyOrdered == nil {
s.SingleKeyOrdered = &Model_SingleKeyOrdered_OrderedMap{}
}
return s.SingleKeyOrdered
}

// AppendNewSingleKeyOrdered creates a new entry in the SingleKeyOrdered
// ordered map of the Model struct. The keys of the list are
// populated from the input arguments.
Expand Down
11 changes: 11 additions & 0 deletions gogen/testdata/structs/openconfig-withlist.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ func (t *Model) RenameSingleKey(oldK, newK string) error {
return nil
}

// GetOrCreateSingleKeyOrderedMap returns the ordered map field
// SingleKeyOrdered from Model.
//
// It initializes the field if not already initialized.
func (s *Model) GetOrCreateSingleKeyOrderedMap() *Model_SingleKeyOrdered_OrderedMap {
if s.SingleKeyOrdered == nil {
s.SingleKeyOrdered = &Model_SingleKeyOrdered_OrderedMap{}
}
return s.SingleKeyOrdered
}

// AppendNewSingleKeyOrdered creates a new entry in the SingleKeyOrdered
// ordered map of the Model struct. The keys of the list are
// populated from the input arguments.
Expand Down
39 changes: 36 additions & 3 deletions gogen/unordered_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ func (t *{{ .Receiver }}) Get{{ .ListName }}(
}
`)

// goGetOrCreateListTemplate defines a template for a function that, for a
// goGetOrCreateListElementTemplate defines a template for a function that, for a
// particular list key, gets an existing map value, or creates it if it doesn't
// exist.
goGetOrCreateListTemplate = mustMakeTemplate("getOrCreateList", `
goGetOrCreateListElementTemplate = mustMakeTemplate("getOrCreateListElement", `
// GetOrCreate{{ .ListName }} retrieves the value with the specified keys from
// the receiver {{ .Receiver }}. If the entry does not exist, then it is created.
// It returns the existing or new list member.
Expand Down Expand Up @@ -240,6 +240,36 @@ func (t *{{ .Receiver }}) GetOrCreate{{ .ListName }}(
}
return v
}
`)

// goGetOrCreateListTemplate defines a template for a function that
// returns the current list. It also creates it if it doesn't exist.
goGetOrCreateListTemplate = mustMakeTemplate("getOrCreateList", `
// GetOrCreate{{ .ListName }}Map returns the list (map) from {{ .Receiver }}.
//
// It initializes the field if not already initialized.
func (t *{{ .Receiver }}) GetOrCreate{{ .ListName }}Map() {{ if ne .KeyStruct "" -}}
map[{{ .KeyStruct }}]*{{ .ListType }}
{{- else }}
{{- $listName := .ListName -}}
{{- $listType := .ListType -}}
{{- range $key := .Keys -}}
map[{{ $key.Type }}]*{{ $listType }}
{{- end }}
{{- end }} {
if t.{{ .ListName }} == nil {
{{- if ne .KeyStruct "" }}
t.{{ .ListName }} = make(map[{{ .KeyStruct }}]*{{ .ListType }})
{{- else }}
{{- $listName := .ListName -}}
{{- $listType := .ListType -}}
{{- range $key := .Keys }}
t.{{ $listName }} = make(map[{{ $key.Type }}]*{{ $listType }})
{{- end }}
{{- end }}
}
return t.{{ .ListName }}
}
`)

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

// generateListGetter generates a getter function for members of the a YANG list
Expand Down
43 changes: 43 additions & 0 deletions integration_tests/schemaops/ctestschema/ctestschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ func (t *Device) RenameUnorderedList(oldK, newK string) error {
return nil
}

// GetOrCreateUnorderedListMap returns the list (map) from Device.
//
// It initializes the field if not already initialized.
func (t *Device) GetOrCreateUnorderedListMap() map[string]*UnorderedList {
if t.UnorderedList == nil {
t.UnorderedList = make(map[string]*UnorderedList)
}
return t.UnorderedList
}

// GetOrCreateUnorderedList retrieves the value with the specified keys from
// the receiver Device. If the entry does not exist, then it is created.
// It returns the existing or new list member.
Expand Down Expand Up @@ -296,6 +306,17 @@ func (t *Device) GetOtherData() *OtherData {
return nil
}

// GetOrCreateOrderedListMap returns the ordered map field
// OrderedList from Device.
//
// It initializes the field if not already initialized.
func (s *Device) GetOrCreateOrderedListMap() *OrderedList_OrderedMap {
if s.OrderedList == nil {
s.OrderedList = &OrderedList_OrderedMap{}
}
return s.OrderedList
}

// AppendNewOrderedList creates a new entry in the OrderedList
// ordered map of the Device struct. The keys of the list are
// populated from the input arguments.
Expand Down Expand Up @@ -458,6 +479,17 @@ func (o *OrderedList_OrderedMap) AppendNew(Key string) (*OrderedList, error) {
return newElement, nil
}

// GetOrCreateOrderedMultikeyedListMap returns the ordered map field
// OrderedMultikeyedList from Device.
//
// It initializes the field if not already initialized.
func (s *Device) GetOrCreateOrderedMultikeyedListMap() *OrderedMultikeyedList_OrderedMap {
if s.OrderedMultikeyedList == nil {
s.OrderedMultikeyedList = &OrderedMultikeyedList_OrderedMap{}
}
return s.OrderedMultikeyedList
}

// AppendNewOrderedMultikeyedList creates a new entry in the OrderedMultikeyedList
// ordered map of the Device struct. The keys of the list are
// populated from the input arguments.
Expand Down Expand Up @@ -762,6 +794,17 @@ func (t *OrderedList) GetValue() string {
return *t.Value
}

// GetOrCreateOrderedListMap returns the ordered map field
// OrderedList from OrderedList.
//
// It initializes the field if not already initialized.
func (s *OrderedList) GetOrCreateOrderedListMap() *OrderedList_OrderedList_OrderedMap {
if s.OrderedList == nil {
s.OrderedList = &OrderedList_OrderedList_OrderedMap{}
}
return s.OrderedList
}

// AppendNewOrderedList creates a new entry in the OrderedList
// ordered map of the OrderedList struct. The keys of the list are
// populated from the input arguments.
Expand Down
Loading

0 comments on commit c52408e

Please sign in to comment.