-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples_test.go
154 lines (140 loc) · 3.57 KB
/
examples_test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package scheyaml
import (
"fmt"
"github.com/kaptinlin/jsonschema"
)
func ExampleSchemaToYAML() {
input := `{
"type": "object",
"properties": {
"name": {
"type": "string",
"default": "Robin",
"description": "The name of the customer"
},
"beverages": {
"type": "array",
"description": "A list of beverages the customer has consumed",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the beverage",
"examples": ["Coffee", "Tea", "Cappucino"]
},
"price": {
"type": "number",
"description": "The price of the product",
"default": 4.5
}
}
}
}
}
}`
compiler := jsonschema.NewCompiler()
schema, _ := compiler.Compile([]byte(input))
result, _ := SchemaToYAML(schema)
fmt.Println(string(result))
// Output:
// # A list of beverages the customer has consumed
// beverages:
// - # The name of the beverage
// #
// # Examples:
// # - Coffee
// # - Tea
// # - Cappucino
// name: null # TODO: Fill this in
// # The price of the product
// price: 4.5
// # The name of the customer
// name: Robin
}
func ExampleSchemaToYAML_withOverrideValues() {
input := `{
"type": "object",
"properties": {
"name": {
"type": "string",
"default": "Robin",
"description": "The name of the customer"
},
"previous_orders": {
"type": "array",
"items": {
"type": "string"
},
"description": "names of beverages the customer has consumed"
},
"beverages": {
"type": "array",
"description": "A list of beverages the customer has consumed",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the beverage",
"examples": ["Coffee", "Tea", "Cappucino"]
},
"price": {
"type": "number",
"description": "The price of the product",
"default": 4.5
},
"description": {
"type": "string"
}
}
}
}
}
}`
overrides := map[string]any{
"name": "John",
"previous_orders": []string{"Water", "Tea"},
"beverages": []any{
map[string]any{"name": "Coffee"},
map[string]any{"name": "Beer"},
},
}
todoComment := "Do something with this"
compiler := jsonschema.NewCompiler()
schema, _ := compiler.Compile([]byte(input))
result, err := SchemaToYAML(schema, WithIndent(2), WithOverrideValues(overrides), WithTODOComment(todoComment))
if err != nil {
fmt.Println(err)
}
fmt.Println(string(result))
// Output:
// # A list of beverages the customer has consumed
// beverages:
// - description: null # Do something with this
// # The name of the beverage
// #
// # Examples:
// # - Coffee
// # - Tea
// # - Cappucino
// name: Coffee
// # The price of the product
// price: 4.5
// - description: null # Do something with this
// # The name of the beverage
// #
// # Examples:
// # - Coffee
// # - Tea
// # - Cappucino
// name: Beer
// # The price of the product
// price: 4.5
// # The name of the customer
// name: John
// # names of beverages the customer has consumed
// previous_orders:
// - Water
// - Tea
}