Skip to content

Commit

Permalink
ensure example works for pointer types
Browse files Browse the repository at this point in the history
matryer committed Aug 13, 2021
1 parent 87591d3 commit 2232d16
Showing 3 changed files with 15 additions and 28 deletions.
22 changes: 3 additions & 19 deletions parser/example.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package parser

import (
"encoding/json"
"html/template"
)
import "fmt"

// Example generates an object that is a realistic example
// of this object.
@@ -13,9 +10,9 @@ func (d *Definition) Example(o Object) (map[string]interface{}, error) {
obj := make(map[string]interface{})
for _, field := range o.Fields {
if field.Type.IsObject {
subobj, err := d.Object(field.Type.TypeName)
subobj, err := d.Object(field.Type.CleanObjectName)
if err != nil {
return nil, err
return nil, fmt.Errorf("Object(%q): %w", field.Type.CleanObjectName, err)
}
example, err := d.Example(*subobj)
if err != nil {
@@ -36,16 +33,3 @@ func (d *Definition) Example(o Object) (map[string]interface{}, error) {
}
return obj, nil
}

// ExampleJSON is like Example, but returns a JSON string.
func (d *Definition) ExampleJSON(o Object) (template.HTML, error) {
example, err := d.Example(o)
if err != nil {
return "", err
}
exampleBytes, err := json.MarshalIndent(example, "", "\t")
if err != nil {
return "", err
}
return template.HTML(exampleBytes), nil
}
19 changes: 11 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -137,9 +137,12 @@ type FieldTag struct {
// FieldType holds information about the type of data that this
// Field stores.
type FieldType struct {
TypeID string `json:"typeID"`
TypeName string `json:"typeName"`
ObjectName string `json:"objectName"`
TypeID string `json:"typeID"`
TypeName string `json:"typeName"`
ObjectName string `json:"objectName"`
// CleanObjectName is the ObjectName with * removed
// for pointer types.
CleanObjectName string `json:"cleanObjectName"`
ObjectNameLowerCamel string `json:"objectNameLowerCamel"`
Multiple bool `json:"multiple"`
Package string `json:"package"`
@@ -435,15 +438,15 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT
ftype.ObjectName = types.TypeString(originalTyp, func(other *types.Package) string { return "" })
ftype.ObjectNameLowerCamel = camelizeDown(ftype.ObjectName)
ftype.TypeID = pkgPath + "." + ftype.ObjectName
typeWithoutPointer := strings.TrimPrefix(ftype.TypeName, "*")
ftype.TSType = typeWithoutPointer
ftype.JSType = typeWithoutPointer
ftype.SwiftType = typeWithoutPointer
ftype.CleanObjectName = strings.TrimPrefix(ftype.TypeName, "*")
ftype.TSType = ftype.CleanObjectName
ftype.JSType = ftype.CleanObjectName
ftype.SwiftType = ftype.CleanObjectName
if ftype.IsObject {
ftype.JSType = "object"
//ftype.SwiftType = "Any"
} else {
switch typeWithoutPointer {
switch ftype.CleanObjectName {
case "interface{}":
ftype.JSType = "any"
ftype.SwiftType = "Any"
2 changes: 1 addition & 1 deletion render/render.go
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ func Render(template string, def parser.Definition, params map[string]interface{
}

func toJSONHelper(v interface{}) (template.HTML, error) {
b, err := json.Marshal(v)
b, err := json.MarshalIndent(v, "", "\t")
if err != nil {
return "", err
}

0 comments on commit 2232d16

Please sign in to comment.