Skip to content

Commit

Permalink
Prepare for auto-releaser infra (#554)
Browse files Browse the repository at this point in the history
## Changes
- make `code.Field` always pointers
- added codegen toolchain config
- added changelog template
- refactored code generator to a dedicated package

## Tests
- prepare release for python sdk
  • Loading branch information
nfx authored Jul 17, 2023
1 parent 7e680c5 commit 546814a
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 121 deletions.
11 changes: 10 additions & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,14 @@
},
"examples": {
".codegen/examples_test.go.tmpl": "service/{{.Package}}/{{.SnakeName}}_usage_test.go"
},
"version": {
"version/version.go": "const Version = \"$VERSION\""
},
"toolchain": {
"required": ["go"],
"post_generate": [
"go test -short ./..."
]
}
}
}
55 changes: 55 additions & 0 deletions .codegen/changelog.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Version changelog

## {{.Version}}

{{range .Changes -}}
* {{.}}.
{{end}}{{- if .ApiChanges}}
API Changes:
{{range .ApiChanges}}
* {{.Action}} {{template "what" .}}{{if .Extra}} {{.Extra}}{{with .Other}} {{template "what" .}}{{end}}{{end}}.
{{- end}}

OpenAPI SHA: {{.Sha}}, Date: {{.Changed}}
{{- end}}{{if .DependencyUpdates}}
Dependency updates:
{{range .DependencyUpdates}}
* {{.}}.
{{- end -}}
{{end}}

## {{.PrevVersion}}

{{- define "what" -}}
{{if eq .X "package" -}}
[{{.Package.Name}}](https://pkg.go.dev/github.com/databricks/databricks-sdk-go/service/{{.Package.Name}}) package
{{- else if eq .X "service" -}}
{{template "service" .Service}}
{{- else if eq .X "method" -}}
`{{.Method.PascalName}}` method for {{template "service" .Method.Service}}
{{- else if eq .X "entity" -}}
{{template "entity" .Entity}}
{{- else if eq .X "field" -}}
`{{.Field.PascalName}}` field for {{template "entity" .Field.Of}}
{{- end}}
{{- end -}}

{{- define "service" -}}
[{{if .IsAccounts}}a{{else}}w{{end}}.{{.PascalName}}](https://pkg.go.dev/github.com/databricks/databricks-sdk-go/service/{{.Package.Name}}#{{.PascalName}}API) {{if .IsAccounts}}account{{else}}workspace{{end}}-level service
{{- end -}}

{{- define "entity" -}}
{{- if not . }}any /* ERROR */
{{- else if .IsEmpty}}`any`
{{- else if .PascalName}}[{{.Package.Name}}.{{.PascalName}}](https://pkg.go.dev/github.com/databricks/databricks-sdk-go/service/{{.Package.Name}}#{{.PascalName}})
{{- else if .IsAny}}`any`
{{- else if .IsString}}`string`
{{- else if .IsBool}}`bool`
{{- else if .IsInt64}}`int64`
{{- else if .IsFloat64}}`float64`
{{- else if .IsInt}}`int`
{{- else if .ArrayValue }}[]{{template "entity" .ArrayValue}}
{{- else if .MapValue }}map[string]{{template "entity" .MapValue}}
{{- else}}[{{.Package.Name}}.{{.PascalName}}](https://pkg.go.dev/github.com/databricks/databricks-sdk-go/service/{{.Package.Name}}#{{.PascalName}})
{{- end -}}
{{- end -}}
47 changes: 40 additions & 7 deletions openapi/code/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Entity struct {

// if entity has required fields, this is the order of them
RequiredOrder []string
fields map[string]Field
fields map[string]*Field

// Schema references the OpenAPI schema this entity was created from.
Schema *openapi.Schema
Expand Down Expand Up @@ -102,7 +102,7 @@ func (e *Entity) Field(name string) *Field {
return nil
}
field.Of = e
return &field
return field
}

// Given a list of field names, return the list of *Field objects which result
Expand Down Expand Up @@ -136,7 +136,7 @@ func (e *Entity) IsExternal() bool {
return e.Package != nil && len(e.Package.types) == 0
}

func (e *Entity) RequiredFields() (fields []Field) {
func (e *Entity) RequiredFields() (fields []*Field) {
for _, r := range e.RequiredOrder {
v := e.fields[r]
v.Of = e
Expand All @@ -145,7 +145,7 @@ func (e *Entity) RequiredFields() (fields []Field) {
return
}

func (e *Entity) NonRequiredFields() (fields []Field) {
func (e *Entity) NonRequiredFields() (fields []*Field) {
required := map[string]bool{}
for _, r := range e.RequiredOrder {
required[r] = true
Expand All @@ -158,19 +158,19 @@ func (e *Entity) NonRequiredFields() (fields []Field) {
v.Of = e
fields = append(fields, v)
}
slices.SortFunc(fields, func(a, b Field) bool {
slices.SortFunc(fields, func(a, b *Field) bool {
return a.CamelName() < b.CamelName()
})
return
}

// Fields returns sorted slice of field representations
func (e *Entity) Fields() (fields []Field) {
func (e *Entity) Fields() (fields []*Field) {
for _, v := range e.fields {
v.Of = e
fields = append(fields, v)
}
slices.SortFunc(fields, func(a, b Field) bool {
slices.SortFunc(fields, func(a, b *Field) bool {
return a.CamelName() < b.CamelName()
})
return fields
Expand Down Expand Up @@ -252,3 +252,36 @@ func (e *Entity) IsPrivatePreview() bool {
func (e *Entity) IsPublicPreview() bool {
return e.Schema != nil && isPublicPreview(&e.Schema.Node)
}

func (e *Entity) IsRequest() bool {
for _, svc := range e.Package.services {
for _, m := range svc.methods {
if m.Request == e {
return true
}
}
}
return false
}

func (e *Entity) IsResponse() bool {
for _, svc := range e.Package.services {
for _, m := range svc.methods {
if m.Response == e {
return true
}
}
}
return false
}

func (e *Entity) IsReferred() bool {
for _, t := range e.Package.types {
for _, f := range t.fields {
if f.Entity == e {
return true
}
}
}
return false
}
5 changes: 5 additions & 0 deletions openapi/code/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func NewFromFile(name string) (*Batch, error) {
if err != nil {
return nil, fmt.Errorf("spec from %s: %w", name, err)
}
return NewFromSpec(spec)
}

// NewFromSpec converts OpenAPI spec to intermediate representation
func NewFromSpec(spec *openapi.Specification) (*Batch, error) {
batch := Batch{
packages: map[string]*Package{},
}
Expand Down
2 changes: 1 addition & 1 deletion openapi/code/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (m *Method) rpcSingleFields() (params []Field) {
// TODO: explicitly annotate with x-databricks-shortcut
return nil
}
return []Field{m.Request.Fields()[0]}
return []Field{*m.Request.Fields()[0]}
}

func (m *Method) requestShortcutFields() []Field {
Expand Down
9 changes: 5 additions & 4 deletions openapi/code/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ func (pkg *Package) schemaToEntity(s *openapi.Schema, path []string, hasName boo
Named: Named{
Description: s.Description,
},
Schema: s,
enum: map[string]EnumEntry{},
Schema: s,
Package: pkg,
enum: map[string]EnumEntry{},
}
// pull embedded types up, if they can be defined at package level
if s.IsDefinable() && !hasName {
Expand Down Expand Up @@ -183,7 +184,7 @@ func (pkg *Package) schemaToEntity(s *openapi.Schema, path []string, hasName boo

// makeObject converts OpenAPI Schema into type representation
func (pkg *Package) makeObject(e *Entity, s *openapi.Schema, path []string) *Entity {
e.fields = map[string]Field{}
e.fields = map[string]*Field{}
required := map[string]bool{}
for _, v := range s.Required {
required[v] = true
Expand All @@ -196,7 +197,7 @@ func (pkg *Package) makeObject(e *Entity, s *openapi.Schema, path []string) *Ent
}
}
named := Named{k, v.Description}
e.fields[k] = Field{
e.fields[k] = &Field{
Named: named,
Entity: pkg.schemaToEntity(v, append(path, named.PascalName()), false),
Required: required[k],
Expand Down
6 changes: 3 additions & 3 deletions openapi/code/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (svc *Service) newRequest(params []openapi.Parameter, op *openapi.Operation
if op.RequestBody == nil && len(params) == 0 {
return nil
}
request := &Entity{fields: map[string]Field{}}
request := &Entity{fields: map[string]*Field{}}
if op.RequestBody != nil {
request = svc.Package.schemaToEntity(op.RequestBody.Schema(), []string{op.Name()}, true)
}
Expand All @@ -155,7 +155,7 @@ func (svc *Service) newRequest(params []openapi.Parameter, op *openapi.Operation
}
field, exists := request.fields[param.Name]
if !exists {
field = *param
field = param
}
field.IsPath = param.IsPath
field.IsQuery = param.IsQuery
Expand Down Expand Up @@ -226,7 +226,7 @@ func (svc *Service) paramPath(path string, request *Entity, params []openapi.Par
parts = append(parts, PathPart{prefix, nil, false})
continue
}
parts = append(parts, PathPart{prefix, &field, false})
parts = append(parts, PathPart{prefix, field, false})
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion openapi/code/tmpl_util_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var HelperFuncs = template.FuncMap{
// via errors.Is(err, code.ErrSkipThisFile)
panic(ErrSkipThisFile)
},
"alphanumOnly": func(in []Field) (out []Field) {
"alphanumOnly": func(in []*Field) (out []*Field) {
for _, v := range in {
if !alphanumRE.MatchString(v.Name) {
continue
Expand Down
Loading

0 comments on commit 546814a

Please sign in to comment.