This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_schema.go
More file actions
88 lines (76 loc) · 1.97 KB
/
json_schema.go
File metadata and controls
88 lines (76 loc) · 1.97 KB
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
package avrojson
import (
"encoding/json"
"fmt"
"math/rand"
)
// JsonWrapper is a wrapper which holds
// skeleton for Json Schema and Avro Schema
// data read from Avro schema provided for conversion
type JsonWrapper struct {
JS *JSONSchema
AS *AvroSchema
AvroData []byte
}
// JSONSchema holds all required field to create
// json schema
type JSONSchema struct {
Schema string `json:"$schema"`
ID string `json:"$id"`
Title string `json:"title"`
Description string `json:"description,omitempty"`
Type string `json:"type"`
Properties []*Property `json:"properties"`
}
// Property holds the fields used in json schema
type Property struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Type string `json:"type"`
}
// NewJsonWrapper creates a json wrapper using the data from
// provided avro file
func NewJsonWrapper(avroData []byte) (*JsonWrapper, error) {
js := newJsonSchema()
as := &AvroSchema{}
if len(avroData) < 1 {
return nil, fmt.Errorf("empty avro data received")
}
if err := json.Unmarshal(avroData, as); err != nil {
return nil, err
}
js.Title = as.Name
return &JsonWrapper{
AvroData: avroData,
JS: js,
AS: as,
}, nil
}
func jsonAvroDataTypeMap(jsw *JsonWrapper) {
for _, v := range jsw.JS.Properties {
v.Type = avroJSONMap[v.Type]
}
}
func assignProperties(js *JSONSchema, as *AvroSchema) {
propertyList := make([]*Property, 0, len(as.Fields))
for _, f := range as.Fields {
p := &Property{
Name: f.Name,
Description: f.Doc,
Type: f.Type.(string),
}
propertyList = append(propertyList, p)
}
js.Properties = propertyList
}
const jsonSchema = `https://json-schema.org/draft/2020-12/schema`
var id = fmt.Sprintf("https://example.com/data-%d.schema.json", rand.Int())
const obj = `object`
func newJsonSchema() *JSONSchema {
js := &JSONSchema{
Schema: jsonSchema,
ID: id,
Type: obj,
}
return js
}