/
struct.go
203 lines (188 loc) · 6.29 KB
/
struct.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// Copyright 2022 Md Sulaiman. All rights reserved.
// https://developers.google.com/protocol-buffers/
package structpb
import (
"encoding/base64"
"math"
"unicode/utf8"
"google.golang.org/protobuf/runtime/protoimpl"
)
// NewValue constructs a Value from a general-purpose Go interface.
//
// ╔════════════════════════╤════════════════════════════════════════════╗
// ║ Go type │ Conversion ║
// ╠════════════════════════╪════════════════════════════════════════════╣
// ║ nil │ stored as NullValue ║
// ║ bool │ stored as BoolValue ║
// ║ int, int32, int64 │ stored as NumberValue ║
// ║ uint, uint32, uint64 │ stored as NumberValue ║
// ║ float32, float64 │ stored as NumberValue ║
// ║ string │ stored as StringValue; must be valid UTF-8 ║
// ║ []byte │ stored as StringValue; base64-encoded ║
// ║ map[string]interface{} │ stored as StructValue ║
// ║ []interface{} │ stored as ListValue ║
// ╚════════════════════════╧════════════════════════════════════════════╝
//
// When converting an int64 or uint64 to a NumberValue, numeric precision loss
// is possible since they are stored as a float64.
func NewValue(v interface{}) (*Value, error) {
switch v := v.(type) {
case nil:
return NewNullValue(), nil
case bool:
return NewBoolValue(v), nil
case int:
return NewNumberValue(float64(v)), nil
case int32:
return NewNumberValue(float64(v)), nil
case int64:
return NewNumberValue(float64(v)), nil
case uint:
return NewNumberValue(float64(v)), nil
case uint32:
return NewNumberValue(float64(v)), nil
case uint64:
return NewNumberValue(float64(v)), nil
case float32:
return NewNumberValue(float64(v)), nil
case float64:
return NewNumberValue(float64(v)), nil
case string:
if !utf8.ValidString(v) {
return nil, protoimpl.X.NewError("invalid UTF-8 in string: %q", v)
}
return NewStringValue(v), nil
case []byte:
s := base64.StdEncoding.EncodeToString(v)
return NewStringValue(s), nil
case map[string]interface{}:
v2, err := NewStruct(v)
if err != nil {
return nil, err
}
return NewStructValue(v2), nil
case []interface{}:
v2, err := NewList(v)
if err != nil {
return nil, err
}
return NewListValue(v2), nil
default:
return nil, protoimpl.X.NewError("invalid type: %T", v)
}
}
// NewNullValue constructs a new null Value.
func NewNullValue() *Value {
return &Value{Kind: &Value_NullValue{NullValue: NullValue_NULL_VALUE}}
}
// NewBoolValue constructs a new boolean Value.
func NewBoolValue(v bool) *Value {
return &Value{Kind: &Value_BoolValue{BoolValue: v}}
}
// NewNumberValue constructs a new number Value.
func NewNumberValue(v float64) *Value {
return &Value{Kind: &Value_NumberValue{NumberValue: v}}
}
// NewStringValue constructs a new string Value.
func NewStringValue(v string) *Value {
return &Value{Kind: &Value_StringValue{StringValue: v}}
}
// NewStructValue constructs a new struct Value.
func NewStructValue(v *Struct) *Value {
return &Value{Kind: &Value_StructValue{StructValue: v}}
}
// NewListValue constructs a new list Value.
func NewListValue(v *ListValue) *Value {
return &Value{Kind: &Value_ListValue{ListValue: v}}
}
// AsInterface converts x to a general-purpose Go interface.
//
// Calling Value.MarshalJSON and "encoding/json".Marshal on this output produce
// semantically equivalent JSON (assuming no errors occur).
//
// Floating-point values (i.e., "NaN", "Infinity", and "-Infinity") are
// converted as strings to remain compatible with MarshalJSON.
func (x *Value) AsInterface() interface{} {
switch v := x.GetKind().(type) {
case *Value_NumberValue:
if v != nil {
switch {
case math.IsNaN(v.NumberValue):
return "NaN"
case math.IsInf(v.NumberValue, +1):
return "Infinity"
case math.IsInf(v.NumberValue, -1):
return "-Infinity"
default:
return v.NumberValue
}
}
case *Value_StringValue:
if v != nil {
return v.StringValue
}
case *Value_BoolValue:
if v != nil {
return v.BoolValue
}
case *Value_StructValue:
if v != nil {
return v.StructValue.AsMap()
}
case *Value_ListValue:
if v != nil {
return v.ListValue.AsSlice()
}
}
return nil
}
// NewStruct constructs a Struct from a general-purpose Go map.
// The map keys must be valid UTF-8.
// The map values are converted using NewValue.
func NewStruct(v map[string]interface{}) (*Struct, error) {
x := &Struct{Fields: make(map[string]*Value, len(v))}
for k, v := range v {
if !utf8.ValidString(k) {
return nil, protoimpl.X.NewError("invalid UTF-8 in string: %q", k)
}
var err error
x.Fields[k], err = NewValue(v)
if err != nil {
return nil, err
}
}
return x, nil
}
// AsMap converts x to a general-purpose Go map.
// The map values are converted by calling Value.AsInterface.
func (x *Struct) AsMap() map[string]interface{} {
vs := make(map[string]interface{})
for k, v := range x.GetFields() {
vs[k] = v.AsInterface()
}
return vs
}
// NewList constructs a ListValue from a general-purpose Go slice.
// The slice elements are converted using NewValue.
func NewList(v []interface{}) (*ListValue, error) {
x := &ListValue{Values: make([]*Value, len(v))}
for i, v := range v {
var err error
x.Values[i], err = NewValue(v)
if err != nil {
return nil, err
}
}
return x, nil
}
// AsSlice converts x to a general-purpose Go slice.
// The slice elements are converted by calling Value.AsInterface.
func (x *ListValue) AsSlice() []interface{} {
vs := make([]interface{}, len(x.GetValues()))
for i, v := range x.GetValues() {
vs[i] = v.AsInterface()
}
return vs
}