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

Added more entity-generation utils #257

Merged
merged 3 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ doc:
@go run golang.org/x/tools/cmd/godoc@latest -http=localhost:6060

install-codegen: vendor
@go build -o ~/go/bin/openapi-codegen openapi/gen/main.go
@go build -o ~/go/bin/oac openapi/gen/main.go

gen:
@go run openapi/gen/main.go
Expand Down
20 changes: 20 additions & 0 deletions openapi/code/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ func (e *Entity) Fields() (fields []Field) {
return fields
}

// HasQueryField returns true if any of the fields is from query
func (e *Entity) HasQueryField() bool {
for _, v := range e.fields {
if v.IsQuery {
return true
}
}
return false
}

// HasJsonField returns true if any of the fields is in the body
func (e *Entity) HasJsonField() bool {
for _, v := range e.fields {
if v.IsJson {
return true
}
}
return false
}

// Does this type have x-databricks-id field?
func (e *Entity) HasIdentifierField() bool {
return e.IdentifierField() != nil
Expand Down
4 changes: 4 additions & 0 deletions openapi/code/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func (m *Method) Shortcut() *Shortcut {
}
}

func (m *Method) IsCrudRead() bool {
return m.operation.Crud == "read"
}

// Wait returns definition for long-running operation
func (m *Method) Wait() *Wait {
if m.wait == nil {
Expand Down
13 changes: 11 additions & 2 deletions openapi/code/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,17 @@ func (svc *Service) newRequest(params []openapi.Parameter, op *openapi.Operation
field.IsQuery = param.IsQuery
request.fields[param.Name] = field
if param.Required {
// TODO: figure out what to do with entity+param requests
request.RequiredOrder = append(request.RequiredOrder, param.Name)
var alreadyRequired bool
for _, v := range request.RequiredOrder {
if v == param.Name {
alreadyRequired = true
break
}
}
if !alreadyRequired {
// TODO: figure out what to do with entity+param requests
request.RequiredOrder = append(request.RequiredOrder, param.Name)
}
}
}
if request.Name == "" {
Expand Down
15 changes: 15 additions & 0 deletions openapi/code/tmpl_util_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package code
import (
"errors"
"reflect"
"regexp"
"strings"
"text/template"
)

var ErrSkipThisFile = errors.New("skip generating this file")

var alphanumRE = regexp.MustCompile(`^\w*$`)

var HelperFuncs = template.FuncMap{
"notLast": func(idx int, a interface{}) bool {
return idx+1 != reflect.ValueOf(a).Len()
Expand All @@ -29,4 +32,16 @@ var HelperFuncs = template.FuncMap{
// via errors.Is(err, code.ErrSkipThisFile)
panic(ErrSkipThisFile)
},
"alphanumOnly": func(in []Field) (out []Field) {
for _, v := range in {
if !alphanumRE.MatchString(v.Name) {
continue
}
out = append(out, v)
}
return out
},
"list": func(l ...any) []any {
return l
},
}