-
Notifications
You must be signed in to change notification settings - Fork 33
/
reqSchema.go
98 lines (89 loc) · 2.37 KB
/
reqSchema.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
// Copyright 2017 MSolution.IO
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package req
import (
"fmt"
"io"
"reflect"
"strings"
)
const (
schemaIndent = "\t"
)
// GetSchema writes to `dst` a JSON-like schema of type `typ`. It returns nil
// iff it finished writing the schema. It returns an error instead.
func GetSchema(dst io.Writer, typ reflect.Type) error {
if _, err := dst.Write([]byte("{\n")); err != nil {
return err
}
if err := getSchemaBody(dst, typ); err != nil {
return err
}
if _, err := dst.Write([]byte("}")); err != nil {
return err
}
return nil
}
func getSchemaBody(dst io.Writer, typ reflect.Type) error {
for i, fc := 0, typ.NumField(); i < fc; i++ {
fld := typ.FieldByIndex([]int{i})
if err := getSchemaField(dst, fld, i+1 == fc); err != nil {
return err
}
}
return nil
}
func getSchemaField(dst io.Writer, fld reflect.StructField, last bool) error {
if err := getSchemaFieldKey(dst, fld); err != nil {
return err
}
if err := getSchemaFieldValue(dst, fld, last); err != nil {
return err
}
return nil
}
func getSchemaFieldKey(dst io.Writer, fld reflect.StructField) error {
jsonName := getJsonName(fld)
key := fmt.Sprintf(schemaIndent+`"%s": `, jsonName)
_, err := dst.Write([]byte(key))
return err
}
func getSchemaFieldValue(dst io.Writer, fld reflect.StructField, last bool) error {
var decorated string
var sep string
if isFieldNonZero(fld) {
decorated = "¡%s!"
} else {
decorated = "¿%s?"
}
if last {
sep = ""
} else {
sep = ","
}
decorated = fmt.Sprintf(decorated, fld.Type.String())
value := fmt.Sprintf("%s%s\n", decorated, sep)
_, err := dst.Write([]byte(value))
return err
}
func isFieldNonZero(fld reflect.StructField) bool {
tag := fld.Tag.Get(StructTagName)
tags := strings.Split(tag, ",")
for _, t := range tags {
if t == StructTagNonZero {
return true
}
}
return false
}