Skip to content

Commit

Permalink
support map and multi slice (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 authored Apr 7, 2024
1 parent 35fc1f3 commit 9b1f0c8
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 428 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
47 changes: 18 additions & 29 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,27 @@ import (

// Object represents the object entity from the swagger definition
type Object struct {
IsArray bool `json:"-"`
GoType reflect.Type `json:"-"`
Name string `json:"-"`
Type string `json:"type"`
Description string `json:"description,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
Properties map[string]Property `json:"properties,omitempty"`
Name string `json:"-"`
Type string `json:"type"`
Description string `json:"description,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
Properties map[string]Property `json:"properties,omitempty"`
AdditionalProperties *Property `json:"additionalProperties,omitempty"`
Items *Property `json:"items,omitempty"`
}

// Property represents the property entity from the swagger definition
type Property struct {
GoType reflect.Type `json:"-"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
Format string `json:"format,omitempty"`
Ref string `json:"$ref,omitempty"`
Example string `json:"example,omitempty"`
Items *Items `json:"items,omitempty"`
AddPropertie *AdditionalProperties `json:"additionalProperties,omitempty"`
}

type AdditionalProperties struct {
GoType reflect.Type `json:"-"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
Format string `json:"format,omitempty"`
Ref string `json:"$ref,omitempty"`
Example string `json:"example,omitempty"`
Items *Items `json:"items,omitempty"`
GoType reflect.Type `json:"-"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
Format string `json:"format,omitempty"`
Ref string `json:"$ref,omitempty"`
Example string `json:"example,omitempty"`
Items *Property `json:"items,omitempty"`
AdditionalProperties *Property `json:"additionalProperties,omitempty"`
}

// Contact represents the contact entity from the swagger definition; used by Info
Expand Down Expand Up @@ -252,7 +241,7 @@ func (a *API) addPath(e *Endpoint) {

func (a *API) addDefinition(e *Endpoint) {
if a.Definitions == nil {
a.Definitions = map[string]Object{}
a.Definitions = make(map[string]Object)
}

if e.Parameters != nil {
Expand Down
10 changes: 1 addition & 9 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,9 @@ import (
"github.com/zc2638/swag/types"
)

// Items represents items from the swagger doc
type Items struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Ref string `json:"$ref,omitempty"`
}

// Schema represents a schema from the swagger doc
type Schema struct {
Type string `json:"type,omitempty"`
Items *Items `json:"items,omitempty"`
Items *Property `json:"items,omitempty"`
Ref string `json:"$ref,omitempty"`
Prototype interface{} `json:"-"`
}
Expand Down
Loading

0 comments on commit 9b1f0c8

Please sign in to comment.