Debian Codegraph @

go/types/v1/types.go

raw symbols SCIP
1 package typesv1
2
3 import (
4 "time"
5
6 "google.golang.org/protobuf/proto"
7 durationpb "google.golang.org/protobuf/types/known/durationpb"
8 timestamppb "google.golang.org/protobuf/types/known/timestamppb"
9 )
10
11 func KeyVal(k string, v *Val) *KV {
12 return &KV{Key: k, Value: v}
13 }
14
15 func EqualTypes(a, b *VarType) bool {
16 return proto.Equal(a, b)
17 }
18
19 func EqualVal(a, b *Val) bool {
20 switch ak := a.Kind.(type) {
21 default:
22 return false
23 case *Val_Str:
24 return ak.Str == b.GetStr()
25 case *Val_F64:
26 return ak.F64 == b.GetF64()
27 case *Val_I64:
28 return ak.I64 == b.GetI64()
29 case *Val_Bool:
30 return ak.Bool == b.GetBool()
31 case *Val_Ts:
32 return ak.Ts.AsTime().Equal(b.GetTs().AsTime())
33 case *Val_Dur:
34 return ak.Dur == b.GetDur()
35 case *Val_Arr:
36 if len(ak.Arr.Items) != len(b.GetArr().Items) {
37 return false
38 }
39 for i, ael := range ak.Arr.Items {
40 bel := b.GetArr().Items[i]
41 if !EqualVal(ael, bel) {
42 return false
43 }
44 }
45 return true
46 case *Val_Obj:
47 if len(ak.Obj.Kvs) != len(b.GetObj().Kvs) {
48 return false
49 }
50 // for simplicity, order matters
51 // TODO: make it so the order doesn't matter
52 for i, akv := range ak.Obj.Kvs {
53 bkv := b.GetObj().Kvs[i]
54 if akv.Key != bkv.Key {
55 return false
56 }
57 if !EqualVal(akv.Value, bkv.Value) {
58 return false
59 }
60 }
61 return true
62 case *Val_Map:
63 if len(ak.Map.Entries) != len(b.GetMap().Entries) {
64 return false
65 }
66 // for simplicity, order matters
67 // TODO: make it so the order doesn't matter
68 for i, akv := range ak.Map.Entries {
69 bkv := b.GetMap().Entries[i]
70 if !EqualVal(akv.Key, bkv.Key) {
71 return false
72 }
73 if !EqualVal(akv.Value, bkv.Value) {
74 return false
75 }
76 }
77 return true
78 case *Val_Null:
79 _, ok := b.Kind.(*Val_Null)
80 return ok
81 }
82 }
83
84 func TypeUnknown() *VarType {
85 return &VarType{Type: &VarType_Scalar{Scalar: ScalarType_unknown}}
86 }
87
88 func TypeStr() *VarType {
89 return &VarType{Type: &VarType_Scalar{Scalar: ScalarType_str}}
90 }
91
92 func TypeF64() *VarType {
93 return &VarType{Type: &VarType_Scalar{Scalar: ScalarType_f64}}
94 }
95
96 func TypeI64() *VarType {
97 return &VarType{Type: &VarType_Scalar{Scalar: ScalarType_i64}}
98 }
99
100 func TypeBool() *VarType {
101 return &VarType{Type: &VarType_Scalar{Scalar: ScalarType_bool}}
102 }
103
104 func TypeTimestamp() *VarType {
105 return &VarType{Type: &VarType_Scalar{Scalar: ScalarType_ts}}
106 }
107
108 func TypeDuration() *VarType {
109 return &VarType{Type: &VarType_Scalar{Scalar: ScalarType_dur}}
110 }
111
112 func TypeArr(v ...*VarType) *VarType {
113 atyp := &VarType_ArrayType{Items: &VarType{Type: &VarType_Scalar{Scalar: ScalarType_unknown}}}
114 for i, item := range v {
115 if i == 0 {
116 atyp.Items.Type = item.Type
117 } else if item.Type != atyp.Items.Type {
118 atyp.Items.Type = &VarType_Scalar{Scalar: ScalarType_unknown}
119 break
120 }
121 }
122 return &VarType{Type: &VarType_Array{Array: atyp}}
123 }
124
125 func TypeObj(types map[string]*VarType) *VarType {
126 otyp := &VarType_ObjectType{Kvs: types}
127 return &VarType{Type: &VarType_Object{Object: otyp}}
128 }
129
130 func TypeObjFromKVs(v ...*KV) *VarType {
131 types := make(map[string]*VarType)
132 for _, kv := range v {
133 types[kv.Key] = kv.Value.Type
134 }
135 return TypeObj(types)
136 }
137
138 func TypeMap(k, v *VarType) *VarType {
139 return &VarType{Type: &VarType_Map{Map: &VarType_MapType{Key: k, Value: v}}}
140 }
141
142 func TypeNull() *VarType {
143 return &VarType{Type: &VarType_Null_{}}
144 }
145
146 func ValStr(v string) *Val {
147 typ := &VarType{Type: &VarType_Scalar{Scalar: ScalarType_str}}
148 return &Val{Type: typ, Kind: &Val_Str{Str: v}}
149 }
150
151 func ValF64(v float64) *Val {
152 typ := &VarType{Type: &VarType_Scalar{Scalar: ScalarType_f64}}
153 return &Val{Type: typ, Kind: &Val_F64{F64: v}}
154 }
155
156 func ValI64(v int64) *Val {
157 typ := &VarType{Type: &VarType_Scalar{Scalar: ScalarType_i64}}
158 return &Val{Type: typ, Kind: &Val_I64{I64: v}}
159 }
160
161 func ValBool(v bool) *Val {
162 typ := &VarType{Type: &VarType_Scalar{Scalar: ScalarType_bool}}
163 return &Val{Type: typ, Kind: &Val_Bool{Bool: v}}
164 }
165
166 func ValTime(v time.Time) *Val {
167 typ := &VarType{Type: &VarType_Scalar{Scalar: ScalarType_ts}}
168 return &Val{Type: typ, Kind: &Val_Ts{Ts: timestamppb.New(v)}}
169 }
170
171 func ValTimestamp(v *timestamppb.Timestamp) *Val {
172 typ := &VarType{Type: &VarType_Scalar{Scalar: ScalarType_ts}}
173 return &Val{Type: typ, Kind: &Val_Ts{Ts: v}}
174 }
175
176 func ValDuration(v time.Duration) *Val {
177 typ := &VarType{Type: &VarType_Scalar{Scalar: ScalarType_dur}}
178 return &Val{Type: typ, Kind: &Val_Dur{Dur: durationpb.New(v)}}
179 }
180
181 func ValArr(v ...*Val) *Val {
182 typs := make([]*VarType, 0, len(v))
183 for _, val := range v {
184 typs = append(typs, val.Type)
185 }
186 return &Val{Type: TypeArr(typs...), Kind: &Val_Arr{Arr: &Arr{
187 Items: v,
188 }}}
189 }
190
191 func ValMap(kt, vt *VarType, v ...*Map_Entry) *Val {
192 return &Val{Type: TypeMap(kt, vt), Kind: &Val_Map{Map: &Map{
193 Entries: v,
194 }}}
195 }
196
197 func ValObj(v ...*KV) *Val {
198 return &Val{Type: TypeObjFromKVs(v...), Kind: &Val_Obj{Obj: &Obj{Kvs: v}}}
199 }
200
201 func ValNull() *Val {
202 return &Val{Type: TypeNull(), Kind: &Val_Null{}}
203 }