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

feat: support ptr slice #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 30 additions & 4 deletions jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package jsonschema

import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
Expand All @@ -22,6 +23,7 @@ func (d *Document) Read(variable interface{}) {
d.setDefaultSchema()

value := reflect.ValueOf(variable)

d.read(value.Type(), tagOptions(""))
}

Expand All @@ -42,6 +44,18 @@ func (d *Document) String() string {
return string(json)
}

func PkgName(t reflect.Type) string {
var pkgName string
if t.Kind() == reflect.Struct {
pkgName = fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
} else if t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct {
pkgName = fmt.Sprintf("%s.%s", t.Elem().PkgPath(), t.Elem().Name())
} else if t.Kind() == reflect.Slice {
pkgName = PkgName(t.Elem())
}
return pkgName
}

type property struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Expand All @@ -62,7 +76,7 @@ func (p *property) read(t reflect.Type, opts tagOptions) {

switch kind {
case reflect.Slice:
p.readFromSlice(t)
p.readFromSlice(t, opts)
case reflect.Map:
p.readFromMap(t)
case reflect.Struct:
Expand All @@ -72,13 +86,17 @@ func (p *property) read(t reflect.Type, opts tagOptions) {
}
}

func (p *property) readFromSlice(t reflect.Type) {
func (p *property) readFromSlice(t reflect.Type, opts tagOptions) {
jsType, _, kind := getTypeFromMapping(t.Elem())

if kind == reflect.Uint8 {
p.Type = "string"
} else if jsType != "" {
p.Items = &property{}
p.Items.read(t.Elem(), tagOptions(""))
} else if kind == reflect.Ptr {
p.Items = &property{}
p.Items.read(t.Elem(), opts)
}
}

Expand All @@ -98,6 +116,8 @@ func (p *property) readFromStruct(t reflect.Type) {
p.Properties = make(map[string]*property, 0)
p.AdditionalProperties = false

pkgName := PkgName(t)

count := t.NumField()
for i := 0; i < count; i++ {
field := t.Field(i)
Expand All @@ -124,11 +144,17 @@ func (p *property) readFromStruct(t reflect.Type) {
}

p.Properties[name] = &property{}
p.Properties[name].read(field.Type, opts)

if !opts.Contains("omitempty") {
p.Required = append(p.Required, name)
}

// 不支持树状结构的递归
if PkgName(field.Type) == pkgName {
p.Properties[name].Type = "object"
continue
}
p.Properties[name].read(field.Type, opts)

}
}

Expand Down
57 changes: 56 additions & 1 deletion jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type ExampleJSONBasicSlices struct {
Slice []string `json:",foo,omitempty"`
SliceOfInterface []interface{} `json:",foo"`
SliceOfStruct []SliceStruct
SliceOfStructPtr []*SliceStruct
}

func (self *propertySuite) TestLoadSliceAndContains(c *C) {
Expand Down Expand Up @@ -124,9 +125,63 @@ func (self *propertySuite) TestLoadSliceAndContains(c *C) {
},
},
},
"SliceOfStructPtr": &property{
Type: "array",
Items: &property{
Type: "object",
Required: []string{"Value"},
Properties: map[string]*property{
"Value": &property{
Type: "string",
},
},
},
},
},

Required: []string{"SliceOfInterface", "SliceOfStruct"},
Required: []string{"SliceOfInterface", "SliceOfStruct", "SliceOfStructPtr"},
},
})
}

type Node struct {
Next *Node
}

type Tree struct {
Children []*Tree
}

type ExampleJSONTree struct {
Node Node
Tree Tree
}

func (self *propertySuite) TestLoadTree(c *C) {
j := &Document{}
j.Read(&ExampleJSONTree{})

c.Assert(*j, DeepEquals, Document{
Schema: "http://json-schema.org/schema#",
property: property{
Type: "object",
Properties: map[string]*property{
"Node": &property{
Type: "object",
Properties: map[string]*property{
"Next": &property{Type: "object"},
},
Required: []string{"Next"},
},
"Tree": &property{
Type: "object",
Properties: map[string]*property{
"Children": &property{Type: "object"},
},
Required: []string{"Children"},
},
},
Required: []string{"Node", "Tree"},
},
})
}
Expand Down