Skip to content

Commit 382a38d

Browse files
authored
[Internal] Added Service.NamedIdMap (#1016)
## Changes This new function on the service allows to get access to NameIdMap from service instead of list method as it was done previously. Previously in code generation relying on `.List.NameIdMap` could lead to undetermenistic behaviour if there were more than 1 List operation on service. Why this is allowed, service can have only 1 name to id method anyway. Thus, new `Service.NamedIdMap` will find `NamedIdMap` from all methods it has and if there are more than 1 - will panic. ## Tests Used here: - [x] `make test` passing - [x] `make fmt` applied - [x] relevant integration tests applied
1 parent 1ef9931 commit 382a38d

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

openapi/code/service.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,38 @@ func (svc *Service) Methods() (methods []*Method) {
9999
return methods
100100
}
101101

102+
// NamedIdMap allows to retrieve a special NamedIdMap object from all methods
103+
// in the service which returns name-to-id mapping retrieval definition for all
104+
// entities of a type
105+
// If there are multiple NamedIdMap for the service, it will panic, because this is not allowed.
106+
func (svc *Service) NamedIdMap() *NamedIdMap {
107+
entities := make([]*NamedIdMap, 0)
108+
for _, v := range svc.Methods() {
109+
// NamedIdMap is defined only for list operations
110+
if v.Operation.Crud != "list" {
111+
continue
112+
}
113+
114+
n := v.NamedIdMap()
115+
if n != nil {
116+
entities = append(entities, n)
117+
}
118+
}
119+
120+
if len(entities) > 1 {
121+
panic(fmt.Errorf("methods for service %s has more than one NamedIdMap operations", svc.Name))
122+
}
123+
124+
if len(entities) == 0 {
125+
return nil
126+
}
127+
128+
return entities[0]
129+
}
130+
102131
// List returns a method annotated with x-databricks-crud:list
103132
func (svc *Service) List() *Method {
104-
for _, v := range svc.methods {
133+
for _, v := range svc.Methods() {
105134
if v.Operation.Crud == "list" {
106135
return v
107136
}
@@ -111,7 +140,7 @@ func (svc *Service) List() *Method {
111140

112141
// List returns a method annotated with x-databricks-crud:create
113142
func (svc *Service) Create() *Method {
114-
for _, v := range svc.methods {
143+
for _, v := range svc.Methods() {
115144
if v.Operation.Crud == "create" {
116145
return v
117146
}
@@ -121,7 +150,7 @@ func (svc *Service) Create() *Method {
121150

122151
// List returns a method annotated with x-databricks-crud:read
123152
func (svc *Service) Read() *Method {
124-
for _, v := range svc.methods {
153+
for _, v := range svc.Methods() {
125154
if v.Operation.Crud == "read" {
126155
return v
127156
}
@@ -131,7 +160,7 @@ func (svc *Service) Read() *Method {
131160

132161
// List returns a method annotated with x-databricks-crud:update
133162
func (svc *Service) Update() *Method {
134-
for _, v := range svc.methods {
163+
for _, v := range svc.Methods() {
135164
if v.Operation.Crud == "update" {
136165
return v
137166
}
@@ -141,7 +170,7 @@ func (svc *Service) Update() *Method {
141170

142171
// List returns a method annotated with x-databricks-crud:delete
143172
func (svc *Service) Delete() *Method {
144-
for _, v := range svc.methods {
173+
for _, v := range svc.Methods() {
145174
if v.Operation.Crud == "delete" {
146175
return v
147176
}

0 commit comments

Comments
 (0)