Skip to content

Commit

Permalink
Handle properties with question marks
Browse files Browse the repository at this point in the history
The heroku schema now includes the `ca_signed?` and `self_signed?`
properties which schematic attempts to turn into this code which doesn't
compile:

        type SSLCert struct {
                ...
                CaSigned? string `json:"ca_signed?"`
        }

This updates the Go field name generation to turn `foo?` schema
properties into `IsFoo` struct fields to match Go's idioms.
  • Loading branch information
bernerdschaefer committed Oct 12, 2020
1 parent 05b3298 commit 5300f83
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmd/schematic/schematic.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func main() {

code, err := s.Generate()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", code)
log.Fatal(err)
}

Expand Down
11 changes: 11 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var helpers = template.FuncMap{
"initialLow": initialLow,
"methodCap": methodCap,
"asComment": asComment,
"fieldName": fieldName,
"fieldTag": fieldTag,
"params": params,
"requestParams": requestParams,
Expand Down Expand Up @@ -51,6 +52,16 @@ func fieldTag(n string, required bool) string {
return fmt.Sprintf("`%s %s`", jsonTag(n, required), urlTag(n, required))
}

func fieldName(name string) string {
fieldName := initialCap(name)

if strings.HasSuffix(name, "?") {
fieldName = "Is" + strings.TrimSuffix(fieldName, "?")
}

return fieldName
}

func jsonTag(n string, required bool) string {
tags := []string{n}
if !required {
Expand Down
53 changes: 38 additions & 15 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,63 @@ package schematic
import "testing"

var initialCapTests = []struct {
Ident string
Depuncted string
In string
Out string
}{
{
Ident: "provider_id",
Depuncted: "ProviderID",
In: "provider_id",
Out: "ProviderID",
},
{
Ident: "app-identity",
Depuncted: "AppIdentity",
In: "app-identity",
Out: "AppIdentity",
},
{
Ident: "uuid",
Depuncted: "UUID",
In: "uuid",
Out: "UUID",
},
{
Ident: "oauth-client",
Depuncted: "OAuthClient",
In: "oauth-client",
Out: "OAuthClient",
},
{
Ident: "Dyno all",
Depuncted: "DynoAll",
In: "Dyno all",
Out: "DynoAll",
},
}

func TestInitialCap(t *testing.T) {
for i, ict := range initialCapTests {
depuncted := depunct(ict.Ident, true)
if depuncted != ict.Depuncted {
t.Errorf("%d: wants %v, got %v", i, ict.Depuncted, depuncted)
depuncted := depunct(ict.In, true)
if depuncted != ict.Out {
t.Errorf("%d: wants %v, got %v", i, ict.Out, depuncted)
}
}
}

var fieldNameTests = append([]struct {
In string
Out string
}{
{
In: "ca_signed?",
Out: "IsCaSigned",
},
},
initialCapTests...,
)

func TestFieldName(t *testing.T) {
for _, tt := range fieldNameTests {
t.Run(tt.In, func(t *testing.T) {
got := fieldName(tt.In)
if got != tt.Out {
t.Fatalf("got %q want %q", got, tt.Out)
}
})
}
}

var asCommentTests = []struct {
Comment string
Commented string
Expand Down
3 changes: 1 addition & 2 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package templates

import "text/template"

var templates = map[string]string{"field.tmpl": `{{initialCap .Name}} {{.Type}} {{fieldTag .Name .Required}} {{asComment .Definition.Description}}
var templates = map[string]string{"field.tmpl": `{{fieldName .Name}} {{.Type}} {{fieldTag .Name .Required}} {{asComment .Definition.Description}}
`,
"funcs.tmpl": `{{$Name := .Name}}
{{$Def := .Definition}}
Expand Down Expand Up @@ -265,4 +265,3 @@ func Parse(t *template.Template) (*template.Template, error) {
}
return t, nil
}

0 comments on commit 5300f83

Please sign in to comment.