Skip to content

Commit

Permalink
fix readme and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Apr 3, 2024
1 parent 38d9630 commit 57ef418
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 97 deletions.
25 changes: 1 addition & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,7 @@ Golang 1.16+
## Installation

```shell
go get -u github.com/zc2638/[email protected]
```

## Use note:
The time.time type uses jsonTag to parse it into a string type
```go
type XXX struct{
Now time.Time `json:",string"`
}
```

The compatibility is resolved as follows:
```go
type xxx struct {
MapSlicePtr map[string][]*string
MapSlice map[string][]string
MapSliceStructPtr map[string][]*Person
MapSliceStruct map[string][]Person
SliceStructPtr *[]*Person
SliceStruct *[]Person
SliceStringPtr *[]*string
SliceString *[]string
}

go get -u github.com/zc2638/[email protected]
```

**Tip:** As of `v1.2.0`, lower versions are no longer compatible. In order to be compatible with most web frameworks,
Expand Down
25 changes: 1 addition & 24 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,7 @@ Golang 1.16+
## 安装

```shell
go get -u github.com/zc2638/[email protected]
```

## 使用注意:
time.time类型使用jsonTag来将其解析为string类型
```go
type XXX struct{
Now time.Time `json:",string"`
}
```

兼容的解析如下:
```go
type xxx struct {
MapSlicePtr map[string][]*string
MapSlice map[string][]string
MapSliceStructPtr map[string][]*Person
MapSliceStruct map[string][]Person
SliceStructPtr *[]*Person
SliceStruct *[]Person
SliceStringPtr *[]*string
SliceString *[]string
}

go get -u github.com/zc2638/[email protected]
```

**Tip:**`v1.2.0` 开始,低版本不再兼容。为了兼容大部分的web框架,整体架构做了很大的改动。
Expand Down
1 change: 0 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

// Object represents the object entity from the swagger definition
type Object struct {
GoType reflect.Type `json:"-"`
Name string `json:"-"`
Type string `json:"type"`
Description string `json:"description,omitempty"`
Expand Down
1 change: 0 additions & 1 deletion endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

// Schema represents a schema from the swagger doc
type Schema struct {
Type string `json:"type,omitempty"`
Items *Property `json:"items,omitempty"`
Ref string `json:"$ref,omitempty"`
Prototype interface{} `json:"-"`
Expand Down
15 changes: 7 additions & 8 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func buildProperty(t reflect.Type) (map[string]Property, []string) {
jsonTag := field.Tag.Get("json")
if strings.Contains(jsonTag, ",string") {
p.Type = types.String.String()
p.GoType = field.Type
} else {
p = inspect(field.Type)
}
Expand All @@ -115,7 +116,7 @@ func buildProperty(t reflect.Type) (map[string]Property, []string) {
return properties, required
}

func defineObject(v interface{}, desc string) Object {
func defineObject(v interface{}) Object {
var t reflect.Type
switch value := v.(type) {
case reflect.Type:
Expand All @@ -127,13 +128,11 @@ func defineObject(v interface{}, desc string) Object {
kind := t.Kind()
if kind == reflect.Ptr {
// unwrap pointer
return defineObject(t.Elem(), desc)
return defineObject(t.Elem())

Check warning on line 131 in reflect.go

View check run for this annotation

Codecov / codecov/patch

reflect.go#L130-L131

Added lines #L130 - L131 were not covered by tests
}

obj := Object{
GoType: t,
Name: kind.String(),
Description: desc,
Name: kind.String(),
}
switch kind {
case reflect.Slice, reflect.Array:
Expand Down Expand Up @@ -164,7 +163,7 @@ func defineObject(v interface{}, desc string) Object {

func define(v interface{}) map[string]Object {
objMap := make(map[string]Object)
obj := defineObject(v, "")
obj := defineObject(v)
objMap[obj.Name] = obj

dirty := true
Expand All @@ -177,7 +176,7 @@ func define(v interface{}) map[string]Object {
if prop.GoType.Kind() == reflect.Struct {
name := makeName(prop.GoType)
if _, exists := objMap[name]; !exists {
child := defineObject(prop.GoType, prop.Description)
child := defineObject(prop.GoType)
objMap[child.Name] = child
dirty = true
}
Expand All @@ -192,7 +191,7 @@ func define(v interface{}) map[string]Object {

// MakeSchema takes struct or pointer to a struct and returns a Schema instance suitable for use by the swagger doc
func MakeSchema(prototype interface{}) *Schema {
obj := defineObject(prototype, "")
obj := defineObject(prototype)
schema := &Schema{
Prototype: prototype,
Ref: makeRef(obj.Name),
Expand Down
40 changes: 17 additions & 23 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -54,9 +55,9 @@ type Pet struct {
MapSlice map[string][]string
MapSliceStructPtr map[string][]*Person
MapSliceStruct map[string][]Person
SliceStructPtr *[]*Person
SliceStructPtr []*Person
SliceStruct *[]Person
SliceStringPtr *[]*string
SliceStringPtr []*string
SliceString *[]string
MapNestOptions *MapObj `json:"map_nest_options,omitempty"`
}
Expand Down Expand Up @@ -94,12 +95,11 @@ func TestDefine(t *testing.T) {
for k, v := range obj.Properties {
e := expected.Properties[k]
assert.Equal(t, e.Type, v.Type, "expected %v.Type to match", k)
assert.Equal(t, e.Description, v.Description, "expected %v.Required to match", k)
assert.Equal(t, e.Enum, v.Enum, "expected %v.Required to match", k)
assert.Equal(t, e.Format, v.Format, "expected %v.Required to match", k)
assert.Equal(t, e.Ref, v.Ref, "expected %v.Required to match", k)
assert.Equal(t, e.Example, v.Example, "expected %v.Required to match", k)
assert.Equal(t, e.Items, v.Items, "expected %v.Required to match", k)
assert.Equal(t, e.Description, v.Description, "expected %v.Description to match", k)
assert.Equal(t, e.Enum, v.Enum, "expected %v.Enum to match", k)
assert.Equal(t, e.Format, v.Format, "expected %v.Format to match", k)
assert.Equal(t, e.Ref, v.Ref, "expected %v.Ref to match", k)
assert.Equal(t, e.Example, v.Example, "expected %v.Example to match", k)
}
}

Expand Down Expand Up @@ -130,13 +130,15 @@ func TestNotStructDefine(t *testing.T) {
assert.Equal(t, "integer", obj.Type)
assert.Equal(t, "int32", obj.Format)

v = define([]byte{1, 2})
obj, ok = v["uint8"]
if !assert.True(t, ok) {
fmt.Printf("%v", v)
}
assert.Equal(t, "integer", obj.Type)
assert.Equal(t, "int32", obj.Format)
bs := []byte{1, 2}
v = define(bs)
bsName := makeName(reflect.TypeOf(bs))
obj, ok = v[bsName]
assert.Equal(t, true, ok)
assert.Equal(t, "array", obj.Type)
assert.NotEqual(t, nil, obj.Items)
assert.Equal(t, "integer", obj.Items.Type)
assert.Equal(t, "int32", obj.Items.Format)
}

func TestHonorJsonIgnore(t *testing.T) {
Expand All @@ -145,11 +147,3 @@ func TestHonorJsonIgnore(t *testing.T) {
assert.True(t, ok)
assert.Equal(t, 0, len(obj.Properties), "expected zero exposed properties")
}

func TestMakeSchemaType(t *testing.T) {
sliceSchema := MakeSchema([]string{})
assert.Equal(t, "array", sliceSchema.Type, "expect array type but get %s", sliceSchema.Type)

objSchema := MakeSchema(struct{}{})
assert.Equal(t, "", objSchema.Type, "expect array type but get %s", objSchema.Type)
}
17 changes: 1 addition & 16 deletions testdata/pet.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,14 @@
"github.com_zc2638_swag.MapOption": {
"type": "object",
"properties": {
"match_src": {
"type": "string"
},
"name": {
"type": "string"
},
"ops": {
"type": "array",
"items": {
"type": "string"
}
},
"sub_options": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/github.com_zc2638_swag.MapOption"
}
},
"values": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down Expand Up @@ -205,4 +190,4 @@
}
}
}
}
}

0 comments on commit 57ef418

Please sign in to comment.