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

improve generated client code by adding SLO initialism #93

Merged
merged 3 commits into from
Dec 11, 2024
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
26 changes: 13 additions & 13 deletions chronosphere/generated_resources.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 5 additions & 17 deletions chronosphere/generateresources/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ type entityType struct {
SwaggerType string
// SwaggerModel represents the underlying model to use. Will normally be the
// same as SwaggerType
SwaggerModel string
// SwaggerParam is the name of the parameter to use in the swagger client request/response
// types. This will normally be the same as SwaggerType, but will be different for the
// SLO resource, where it is "Slo" instead of "SLO".
SwaggerParam string
SwaggerModel string
FieldName string
SwaggerClient string
SwaggerClientPackage string
Expand All @@ -105,7 +101,6 @@ func newEntityType(a api, r registry.Resource) entityType {
GoType: fmt.Sprintf("%s%s", a.GoPrefix, r.Entity),
SwaggerType: r.Entity,
SwaggerModel: r.Entity,
SwaggerParam: r.Entity,
SwaggerClient: fmt.Sprintf("%s.%s", a.Client, r.Entity),
SwaggerClientPackage: strcase.ToSnake(r.Entity),
DisableDryRun: r.DisableDryRun,
Expand All @@ -119,13 +114,6 @@ func newEntityType(a api, r registry.Resource) entityType {
et.GoType = fmt.Sprintf("%s%s", a.GoPrefix, "ClassicDashboard")
}

// SLOs are named with all caps so we get some funny business with the swagger generated code
// that we have to account for in our generated resources code.
if r.Name == "slo" {
et.SwaggerClientPackage = "s_l_o"
et.SwaggerParam = "Slo"
et.SwaggerClient = fmt.Sprintf("%s.%s", a.Client, et.SwaggerParam)
}
return et
}

Expand Down Expand Up @@ -187,15 +175,15 @@ func ({{.GoType}}) create(
req := &{{.SwaggerClientPackage}}.Create{{.SwaggerType}}Params{
Context: ctx,
Body: &{{.API.Package}}models.{{.API.SwaggerPrefix}}Create{{.SwaggerType}}Request{
{{.SwaggerParam}}: m,
{{.SwaggerType}}: m,
{{ if not .DisableDryRun }} DryRun: dryRun, {{ end }}
},
}
resp, err := clients.{{.SwaggerClient}}.Create{{.SwaggerType}}(req)
if err != nil {
return "", err
}
e := resp.Payload.{{.SwaggerParam}}
e := resp.Payload.{{.SwaggerType}}
if e == nil {
return "", nil
}
Expand All @@ -217,7 +205,7 @@ func ({{.GoType}}) read(
if err != nil {
return nil, err
}
return resp.Payload.{{.SwaggerParam}}, nil
return resp.Payload.{{.SwaggerType}}, nil
}

{{ if not .UpdateUnsupported -}}
Expand All @@ -242,7 +230,7 @@ func ({{.GoType}}) update(
{{ else }}
Body: &{{.API.Package}}models.{{.API.SwaggerPrefix}}Update{{.SwaggerType}}Request{
{{ end }}
{{.SwaggerParam}}: m,
{{.SwaggerType}}: m,
CreateIfMissing: params.createIfMissing,
{{ if not .DisableDryRun }} DryRun: params.dryRun, {{ end }}
},
Expand Down
48 changes: 22 additions & 26 deletions chronosphere/pagination/generatepagination/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,30 @@ type api struct {
}

type entityType struct {
API api
Singular string
Plural string
SwaggerAPIMethodPlural string
SwaggerClient string
SwaggerClientPackage string
SwaggerModel string
Disable bool
API api
Singular string
Plural string
// This will normally be the same as Plural, but some entities have irregular plural forms.
PayloadPlural string
SwaggerClient string
SwaggerClientPackage string
SwaggerModel string
Disable bool
}

func newEntityType(a api, e registry.Resource) entityType {
et := entityType{
API: a,
Singular: e.Entity,
Plural: plural(e.Entity),
SwaggerAPIMethodPlural: plural(e.Entity),
SwaggerClientPackage: strcase.ToSnake(e.Entity),
Disable: e.DisableExportImport,
SwaggerModel: e.Entity,
API: a,
Singular: e.Entity,
Plural: plural(e.Entity),
PayloadPlural: plural(e.Entity),
SwaggerClientPackage: strcase.ToSnake(e.Entity),
Disable: e.DisableExportImport,
SwaggerModel: e.Entity,
}

// SLOs are named with all caps so we get some funny business with the swagger generated code
// that we have to account for in our generated resources code.
if e.Entity == "SLO" {
et.SwaggerClientPackage = "s_l_o"
et.Singular = "Slo"
et.Plural = "Slos"
et.SwaggerAPIMethodPlural = "SLOs"
if e.Name == "slo" {
et.PayloadPlural = "Slos"
}

return et
Expand Down Expand Up @@ -188,7 +184,7 @@ func List{{if .API.RequireUnstable}}Unstable{{end}}{{.Plural}}ByFilter(
ctx context.Context,
client *{{.API.Package}}.Client,
f Filter,
opts... func(*{{.SwaggerClientPackage}}.List{{.SwaggerAPIMethodPlural}}Params),
opts... func(*{{.SwaggerClientPackage}}.List{{.Plural}}Params),
) ([]*{{.API.Package}}models.{{.API.SwaggerPrefix}}{{.SwaggerModel}}, error) {
{{- if .Disable}}
return nil, nil
Expand All @@ -203,7 +199,7 @@ func List{{if .API.RequireUnstable}}Unstable{{end}}{{.Plural}}ByFilter(
result []*{{.API.Package}}models.{{.API.SwaggerPrefix}}{{.SwaggerModel}}
)
for {
p := &{{.SwaggerClientPackage}}.List{{.SwaggerAPIMethodPlural}}Params{
p := &{{.SwaggerClientPackage}}.List{{.Plural}}Params{
Context: ctx,
PageToken: &nextToken,
Slugs: f.Slugs,
Expand All @@ -212,15 +208,15 @@ func List{{if .API.RequireUnstable}}Unstable{{end}}{{.Plural}}ByFilter(
for _, opt := range opts {
opt(p)
}
resp, err := client.{{.Singular}}.List{{.SwaggerAPIMethodPlural}}(p)
resp, err := client.{{.Singular}}.List{{.Plural}}(p)
if err != nil {
return nil, err
}

// If payload or page token aren't set, no next page.
nextToken = ""
if resp.Payload != nil {
for _, v := range resp.Payload.{{.Plural}} {
for _, v := range resp.Payload.{{.PayloadPlural}} {
result = append(result, v)
}
if resp.Payload.Page != nil {
Expand Down
22 changes: 11 additions & 11 deletions chronosphere/pagination/pagination.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading