-
Notifications
You must be signed in to change notification settings - Fork 14
/
struct.go
59 lines (45 loc) · 1.1 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package jsonschema
import (
"reflect"
"strconv"
"github.com/swaggest/refl"
)
var structDefaultDefNameIndex = 0
// Field mimics Go reflect.StructField for purposes of schema reflection.
type Field struct {
Name string
Value interface{}
Tag reflect.StructTag
}
// Struct mimics Go struct to allow schema reflection on virtual struct type.
//
// This can be handy for dynamic values that can not be represented as static Go structures.
type Struct struct {
Title *string
Description *string
Nullable bool
DefName string
Fields []Field
}
// SetTitle sets title.
func (s *Struct) SetTitle(title string) {
s.Title = &title
}
// SetDescription sets description.
func (s *Struct) SetDescription(description string) {
s.Description = &description
}
type withStruct interface {
structPtr() *Struct
}
func (s Struct) structPtr() *Struct {
return &s
}
func (s Struct) names() (string, refl.TypeString) {
defName := s.DefName
if defName == "" {
structDefaultDefNameIndex++
defName = "struct" + strconv.Itoa(structDefaultDefNameIndex)
}
return defName, refl.TypeString("struct." + defName)
}