Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pacedotdev/oto
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.5.0
Choose a base ref
...
head repository: pacedotdev/oto
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.6.0
Choose a head ref
  • 7 commits
  • 8 files changed
  • 1 contributor

Commits on Jul 15, 2020

  1. tweaked json representation

    matryer committed Jul 15, 2020
    Copy the full SHA
    bd7f28b View commit details

Commits on Jul 16, 2020

  1. Copy the full SHA
    86aba64 View commit details

Commits on Jul 21, 2020

  1. parser: capture struct tags

    matryer committed Jul 21, 2020
    Copy the full SHA
    2707b2d View commit details
  2. Copy the full SHA
    e6c2006 View commit details
  3. added thank you to @mgutz

    matryer committed Jul 21, 2020
    Copy the full SHA
    7d10579 View commit details
  4. Copy the full SHA
    405507b View commit details
  5. cleaned up old code

    matryer committed Jul 21, 2020
    Copy the full SHA
    ea90915 View commit details
Showing with 80 additions and 21 deletions.
  1. +6 −0 README.md
  2. +1 −0 go.mod
  3. +2 −0 go.sum
  4. +20 −18 parser.go
  5. +1 −0 parser_test.go
  6. +26 −2 render.go
  7. +23 −0 render_test.go
  8. +1 −1 testdata/services/pleasantries/greeter.go
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -126,4 +126,10 @@ oto \

Within your templates, you may access these strings with `<%= params["key1"] %>`.

## Contributions

Special thank you to:

* @mgutz - for struct tag support

![A PACE. project](pace-footer.png)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ go 1.13

require (
github.com/dustin/go-humanize v1.0.0
github.com/fatih/structtag v1.2.0
github.com/gobuffalo/envy v1.9.0 // indirect
github.com/gobuffalo/flect v0.2.1 // indirect
github.com/gobuffalo/helpers v0.6.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/gobuffalo/envy v1.6.5 h1:X3is06x7v0nW2xiy2yFbbIjwHz57CD6z6MkvqULTCm8=
github.com/gobuffalo/envy v1.6.5/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9kaifseQ=
github.com/gobuffalo/envy v1.9.0 h1:eZR0DuEgVLfeIb1zIKt3bT4YovIMf9O9LXQeCZLXpqE=
38 changes: 20 additions & 18 deletions parser.go
Original file line number Diff line number Diff line change
@@ -18,14 +18,14 @@ var errNotFound = errors.New("not found")
// Definition describes an Oto definition.
type Definition struct {
// PackageName is the name of the package.
PackageName string `json:"packageName,omitempty"`
PackageName string `json:"packageName"`
// Services are the services described in this definition.
Services []Service `json:"services,omitempty"`
Services []Service `json:"services"`
// Objects are the structures that are used throughout this definition.
Objects []Object `json:"objects,omitempty"`
Objects []Object `json:"objects"`
// Imports is a map of Go imports that should be imported into
// Go code.
Imports map[string]string `json:"imports,omitempty"`
Imports map[string]string `json:"imports"`
}

// Object looks up an object by name. Returns errNotFound error
@@ -42,34 +42,35 @@ func (d *Definition) Object(name string) (*Object, error) {

// Service describes a service, akin to an interface in Go.
type Service struct {
Name string `json:"name,omitempty"`
Methods []Method `json:"methods,omitempty"`
Comment string `json:"comment,omitempty"`
Name string `json:"name"`
Methods []Method `json:"methods"`
Comment string `json:"comment"`
}

// Method describes a method that a Service can perform.
type Method struct {
Name string `json:"name,omitempty"`
InputObject FieldType `json:"inputObject,omitempty"`
OutputObject FieldType `json:"outputObject,omitempty"`
Comment string `json:"comment,omitempty"`
Name string `json:"name"`
InputObject FieldType `json:"inputObject"`
OutputObject FieldType `json:"outputObject"`
Comment string `json:"comment"`
}

// Object describes a data structure that is part of this definition.
type Object struct {
TypeID string `json:"typeID"`
Name string `json:"name"`
Imported bool `json:"imported,omitempty"`
Fields []Field `json:"fields,omitempty"`
Comment string `json:"comment,omitempty"`
Imported bool `json:"imported"`
Fields []Field `json:"fields"`
Comment string `json:"comment"`
}

// Field describes the field inside an Object.
type Field struct {
Name string `json:"name,omitempty"`
Type FieldType `json:"type,omitempty"`
OmitEmpty bool `json:"omitEmpty,omitempty"`
Comment string `json:"comment,omitempty"`
Name string `json:"name"`
Type FieldType `json:"type"`
OmitEmpty bool `json:"omitEmpty"`
Comment string `json:"comment"`
Tag string `json:"tag"`
}

// FieldType holds information about the type of data that this
@@ -264,6 +265,7 @@ func (p *parser) parseObject(pkg *packages.Package, o types.Object, v *types.Str
if err != nil {
return err
}
field.Tag = v.Tag(i)
obj.Fields = append(obj.Fields, field)
}
p.def.Objects = append(p.def.Objects, obj)
1 change: 1 addition & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ You will love it.`)
is.Equal(greetInputObject.Fields[0].Type.IsObject, true)
is.Equal(greetInputObject.Fields[0].Type.Multiple, false)
is.Equal(greetInputObject.Fields[0].Type.Package, "github.com/pacedotdev/oto/testdata/services")
is.Equal(greetInputObject.Fields[0].Tag, `tagtest:"value,option1,option2"`)

greetOutputObject, err := def.Object(def.Services[0].Methods[0].OutputObject.TypeName)
is.NoErr(err)
28 changes: 26 additions & 2 deletions render.go
Original file line number Diff line number Diff line change
@@ -6,8 +6,10 @@ import (
"go/doc"
"html/template"

"github.com/fatih/structtag"
"github.com/gobuffalo/plush"
"github.com/markbates/inflect"
"github.com/pkg/errors"
)

var defaultRuleset = inflect.NewDefaultRuleset()
@@ -21,6 +23,7 @@ func render(template string, def Definition, params map[string]interface{}) (str
ctx.Set("json", toJSONHelper)
ctx.Set("format_comment_text", formatCommentText)
ctx.Set("format_comment_html", formatCommentHTML)
ctx.Set("format_tags", formatTags)
s, err := plush.Render(string(template), ctx)
if err != nil {
return "", err
@@ -42,10 +45,10 @@ func formatCommentText(s string) string {
return buf.String()
}

func formatCommentHTML(s string) string {
func formatCommentHTML(s string) template.HTML {
var buf bytes.Buffer
doc.ToHTML(&buf, s, nil)
return buf.String()
return template.HTML(buf.String())
}

// camelizeDown converts a name or other string into a camel case
@@ -59,3 +62,24 @@ func camelizeDown(s string) string {
}
return defaultRuleset.CamelizeDownFirst(s)
}

// formatTags formats a list of struct tag strings into one.
// Will return an error if any of the tag strings are invalid.
func formatTags(tags ...string) (template.HTML, error) {
alltags := &structtag.Tags{}
for _, tag := range tags {
theseTags, err := structtag.Parse(tag)
if err != nil {
return "", errors.Wrapf(err, "parse tags: `%s`", tag)
}
for _, t := range theseTags.Tags() {
alltags.Set(t)
}
}
tagsStr := alltags.String()
if tagsStr == "" {
return "", nil
}
tagsStr = "`" + tagsStr + "`"
return template.HTML(tagsStr), nil
}
23 changes: 23 additions & 0 deletions render_test.go
Original file line number Diff line number Diff line change
@@ -40,3 +40,26 @@ func TestCamelizeDown(t *testing.T) {
}
}
}

func TestFormatTags(t *testing.T) {
is := is.New(t)

trimBackticks := func(s string) string {
is.True(strings.HasPrefix(s, "`"))
is.True(strings.HasSuffix(s, "`"))
return strings.Trim(s, "`")
}

tagStr, err := formatTags(`json:"field,omitempty"`)
is.NoErr(err)
is.Equal(trimBackticks(string(tagStr)), `json:"field,omitempty"`)

tagStr, err = formatTags(`json:"field,omitempty" monkey:"true"`)
is.NoErr(err)
is.Equal(trimBackticks(string(tagStr)), `json:"field,omitempty" monkey:"true"`)

tagStr, err = formatTags(`json:"field,omitempty"`, `monkey:"true"`)
is.NoErr(err)
is.Equal(trimBackticks(string(tagStr)), `json:"field,omitempty" monkey:"true"`)

}
2 changes: 1 addition & 1 deletion testdata/services/pleasantries/greeter.go
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ type GreetResponse struct {
// GetGreetingsRequest is the request object for GreeterService.GetGreetings.
type GetGreetingsRequest struct {
// Page describes which page of data to get.
Page services.Page
Page services.Page `tagtest:"value,option1,option2"`
}

// GetGreetingsResponse is the respponse object for GreeterService.GetGreetings.