File: json.go

package info (click to toggle)
golang-github-mimuret-golang-iij-dpf 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: makefile: 55
file content (142 lines) | stat: -rw-r--r-- 3,460 bytes parent folder | download
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
package api

import (
	"reflect"

	jsoniter "github.com/json-iterator/go"
	"github.com/mimuret/golang-iij-dpf/pkg/meta"
)

var JSON = NewJSONAPIAdapter()

func UnmarshalRead(bs []byte, o interface{}) error {
	return JSON.UnmarshalRead(bs, o)
}

func MarshalCreate(body interface{}) ([]byte, error) {
	return JSON.MarshalCreate(body)
}

func MarshalUpdate(body interface{}) ([]byte, error) {
	return JSON.MarshalUpdate(body)
}

func MarshalApply(body interface{}) ([]byte, error) {
	return JSON.MarshalApply(body)
}

func MarshalOutput(spec Spec) ([]byte, error) {
	return JSON.MarshalOutput(spec)
}

func UnMarshalInput(bs []byte, obj Object) error {
	return JSON.UnMarshalInput(bs, obj)
}

type JSONAPIInterface interface {
	UnmarshalRead(bs []byte, o interface{}) error
	MarshalCreate(body interface{}) ([]byte, error)
	MarshalUpdate(body interface{}) ([]byte, error)
	MarshalApply(body interface{}) ([]byte, error)
}

type JSONFileInterface interface {
	MarshalOutput(spec Spec) ([]byte, error)
	UnMarshalInput(bs []byte, obj Object) error
}

type JSONAPIAdapter struct {
	Read   jsoniter.API
	Update jsoniter.API
	Create jsoniter.API
	Apply  jsoniter.API
	JSON   jsoniter.API
}

func NewJSONAPIAdapter() *JSONAPIAdapter {
	return &JSONAPIAdapter{
		Read: jsoniter.Config{
			EscapeHTML:             true,
			SortMapKeys:            false,
			ValidateJsonRawMessage: true,
			OnlyTaggedField:        true,
			TagKey:                 "read",
		}.Froze(),
		Create: jsoniter.Config{
			EscapeHTML:             true,
			SortMapKeys:            true,
			ValidateJsonRawMessage: true,
			OnlyTaggedField:        true,
			TagKey:                 "create",
		}.Froze(),
		Update: jsoniter.Config{
			EscapeHTML:             true,
			SortMapKeys:            true,
			ValidateJsonRawMessage: true,
			OnlyTaggedField:        true,
			TagKey:                 "update",
		}.Froze(),
		Apply: jsoniter.Config{
			EscapeHTML:             true,
			SortMapKeys:            true,
			ValidateJsonRawMessage: true,
			OnlyTaggedField:        true,
			TagKey:                 "apply",
		}.Froze(),
		JSON: jsoniter.Config{
			EscapeHTML:             true,
			SortMapKeys:            true,
			ValidateJsonRawMessage: true,
			OnlyTaggedField:        false,
			TagKey:                 "json",
		}.Froze(),
	}
}

// Unmarshal api response.
func (j *JSONAPIAdapter) UnmarshalRead(bs []byte, o interface{}) error {
	return j.Read.Unmarshal(bs, o)
}

// Marshal for create request.
func (j *JSONAPIAdapter) MarshalCreate(body interface{}) ([]byte, error) {
	return j.Create.Marshal(body)
}

// Marshal for update request.
func (j *JSONAPIAdapter) MarshalUpdate(body interface{}) ([]byte, error) {
	return j.Update.Marshal(body)
}

// Marshal for apply request.
func (j *JSONAPIAdapter) MarshalApply(body interface{}) ([]byte, error) {
	return j.Apply.Marshal(body)
}

// file format frame.
type OutputFrame struct {
	meta.KindVersion `json:",inline"`
	Resource         Object `json:"resource"`
}

// Marshal for file format.
func (j *JSONAPIAdapter) MarshalOutput(spec Spec) ([]byte, error) {
	t := reflect.TypeOf(spec)
	t = t.Elem()
	out := &OutputFrame{
		KindVersion: meta.KindVersion{
			Kind:       t.Name(),
			APIVersion: spec.GetGroup(),
		},
		Resource: spec,
	}
	return j.JSON.Marshal(out)
}

// UnMarshal for file format.
func (j *JSONAPIAdapter) UnMarshalInput(bs []byte, obj Object) error {
	out := &OutputFrame{
		Resource: obj,
	}
	return j.JSON.Unmarshal(bs, out)
}