Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Added Service.NamedIdMap #1016

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions openapi/code/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,38 @@ func (svc *Service) Methods() (methods []*Method) {
return methods
}

// NamedIdMap allows to retrieve a special NamedIdMap object from all methods
// in the service which returns name-to-id mapping retrieval definition for all
// entities of a type
// If there are multiple NamedIdMap for the service, it will panic, because this is not allowed.
Comment on lines +102 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The description wasn't super clear to me. Is this an accurate rewrite?

Suggested change
// NamedIdMap allows to retrieve a special NamedIdMap object from all methods
// in the service which returns name-to-id mapping retrieval definition for all
// entities of a type
// If there are multiple NamedIdMap for the service, it will panic, because this is not allowed.
// NamedIdMap returns the NamedIdMap corresponding to the service's list method
// or nil if the service doesn't have a list method. This function panics if the
// service has more than one list method as this is currently not supported.

func (svc *Service) NamedIdMap() *NamedIdMap {
entities := make([]*NamedIdMap, 0)
for _, v := range svc.Methods() {
// NamedIdMap is defined only for list operations
if v.Operation.Crud != "list" {
continue
}

n := v.NamedIdMap()
if n != nil {
Comment on lines +114 to +115
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] this makes it clear that n is not meant to be used outside of this if.

Suggested change
n := v.NamedIdMap()
if n != nil {
if n := v.NamedIdMap(); n != nil {

entities = append(entities, n)
}
}

if len(entities) > 1 {
panic(fmt.Errorf("methods for service %s has more than one NamedIdMap operations", svc.Name))
}

if len(entities) == 0 {
return nil
}

return entities[0]
}

// List returns a method annotated with x-databricks-crud:list
func (svc *Service) List() *Method {
for _, v := range svc.methods {
for _, v := range svc.Methods() {
if v.Operation.Crud == "list" {
return v
}
Expand All @@ -111,7 +140,7 @@ func (svc *Service) List() *Method {

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

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

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

// List returns a method annotated with x-databricks-crud:delete
func (svc *Service) Delete() *Method {
for _, v := range svc.methods {
for _, v := range svc.Methods() {
if v.Operation.Crud == "delete" {
return v
}
Expand Down
Loading