-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
105 lines (99 loc) · 2.59 KB
/
type.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package openapi
import (
"fmt"
"github.com/getkin/kin-openapi/openapi3"
"github.com/specgen-io/specgen/v2/goven/spec"
"strings"
)
func specType(schema *openapi3.SchemaRef, required bool) *spec.Type {
return &spec.Type{*specTypeDef(schema, required), nil}
}
func specTypeDef(schema *openapi3.SchemaRef, required bool) *spec.TypeDef {
if !required {
return spec.Nullable(specTypeDef(schema, true))
}
if schema.Ref != "" {
return spec.Plain(strings.TrimPrefix(schema.Ref, "#/components/schemas/"))
} else {
switch schema.Value.Type {
case TypeArray:
return spec.Array(specTypeDef(schema.Value.Items, true))
case TypeObject:
if schema.Value.AdditionalProperties != nil {
return spec.Map(specTypeDef(schema.Value.AdditionalProperties, true))
} else {
panic(fmt.Sprintf(`object schema is not supported yet`))
}
return spec.Plain("object")
default:
return specPlainType(schema.Value.Type, schema.Value.Format)
}
}
}
func specPlainType(typ string, format string) *spec.TypeDef {
switch typ {
case TypeBoolean:
switch format {
case "":
return spec.Plain(spec.TypeBoolean)
default:
panic(fmt.Sprintf("Unknown format of boolean: %s", format))
}
case TypeString:
switch format {
case "":
return spec.Plain(spec.TypeString)
case FormatUuid:
return spec.Plain(spec.TypeUuid)
case FormatDate:
return spec.Plain(spec.TypeDate)
case FormatDateTime:
return spec.Plain(spec.TypeDateTime)
default:
panic(fmt.Sprintf("Unknown format of string: %s", format))
}
case TypeNumber:
switch format {
case FormatFloat:
return spec.Plain(spec.TypeFloat)
case FormatDouble:
return spec.Plain(spec.TypeDouble)
case FormatDecimal:
return spec.Plain(spec.TypeDecimal)
default:
panic(fmt.Sprintf("Unknown format of number: %s", format))
}
case TypeInteger:
switch format {
case FormatInt64:
return spec.Plain(spec.TypeInt64)
case FormatInt32:
return spec.Plain(spec.TypeInt32)
case "":
return spec.Plain(spec.TypeInt32)
default:
panic(fmt.Sprintf("Unknown format of integer: %s", format))
}
default:
panic(fmt.Sprintf("Unknown type: %s", typ))
}
return nil
}
const (
TypeArray string = "array"
TypeObject string = "object"
TypeInteger string = "integer"
TypeNumber string = "number"
TypeString string = "string"
TypeBoolean string = "boolean"
)
const (
FormatInt64 string = "int64"
FormatInt32 string = "int32"
FormatFloat string = "float"
FormatDouble string = "double"
FormatDecimal string = "decimal"
FormatUuid string = "uuid"
FormatDate string = "date"
FormatDateTime string = "date-time"
)