1 // Generated by the protocol buffer compiler.  DO NOT EDIT!
2 // source: google/protobuf/struct.proto
3 module google.protobuf.struct_;
4 
5 import std.json : JSONValue;
6 import google.protobuf;
7 
8 class Struct
9 {
10     @Proto(1) Value[string] fields = protoDefaultValue!(Value[string]);
11 
12     this()
13     {
14     }
15 
16     this(JSONValue value)
17     {
18         fromJSONValue(value);
19     }
20 
21     JSONValue toJSONValue()()
22     {
23         import std.array : assocArray;
24         import std.algorithm : map;
25         import std.typecons : tuple;
26 
27         return JSONValue(fields.byKeyValue.map!(a => tuple(a.key, a.value.toJSONValue)).assocArray);
28     }
29 
30     auto fromJSONValue()(JSONValue value)
31     {
32         import std.array : assocArray;
33         import std.algorithm : map;
34         import std.exception : enforce;
35         import std.json : JSON_TYPE;
36         import std.typecons : tuple;
37 
38         if (value.type == JSON_TYPE.NULL)
39         {
40             fields = protoDefaultValue!(Value[string]);
41             return this;
42         }
43 
44         enforce!ProtobufException(value.type == JSON_TYPE.OBJECT, "JSON object expected");
45         fields = value.object.byKeyValue.map!(a => tuple(a.key, new Value(a.value))).assocArray;
46 
47         return this;
48     }
49 }
50 
51 class Value
52 {
53     enum KindCase
54     {
55         kindNotSet = 0,
56         nullValue = 1,
57         numberValue = 2,
58         stringValue = 3,
59         boolValue = 4,
60         structValue = 5,
61         listValue = 6,
62     }
63     KindCase _kindCase = KindCase.kindNotSet;
64     @property KindCase kindCase() { return _kindCase; }
65     void clearKind() { _kindCase = KindCase.kindNotSet; }
66     @Oneof("_kindCase") union
67     {
68         @Proto(1) NullValue _nullValue = protoDefaultValue!NullValue; mixin(oneofAccessors!_nullValue);
69         @Proto(2) double _numberValue; mixin(oneofAccessors!_numberValue);
70         @Proto(3) string _stringValue; mixin(oneofAccessors!_stringValue);
71         @Proto(4) bool _boolValue; mixin(oneofAccessors!_boolValue);
72         @Proto(5) Struct _structValue; mixin(oneofAccessors!_structValue);
73         @Proto(6) ListValue _listValue; mixin(oneofAccessors!_listValue);
74     }
75 
76     this()
77     {
78     }
79 
80     this(JSONValue jsonValue)
81     {
82         fromJSONValue(jsonValue);
83     }
84 
85     override bool opEquals(Object o)
86     {
87         auto other = cast(Value) o;
88         if (other is null)
89             return false;
90 
91         if (kindCase != other.kindCase)
92             return false;
93 
94         final switch (kindCase)
95         {
96         case KindCase.kindNotSet:
97             return true;
98         case KindCase.nullValue:
99             return nullValue == other.nullValue;
100         case KindCase.numberValue:
101             return numberValue == other.numberValue;
102         case KindCase.stringValue:
103             return stringValue == other.stringValue;
104         case KindCase.boolValue:
105             return boolValue == other.boolValue;
106         case KindCase.structValue:
107             return structValue == other.structValue;
108         case KindCase.listValue:
109             return listValue == other.listValue;
110         }
111     }
112 
113     JSONValue toJSONValue()()
114     {
115         import std.array : array, assocArray;
116         import std.algorithm : map;
117         import std.typecons : tuple;
118 
119         final switch (kindCase)
120         {
121         case KindCase.kindNotSet:
122             return JSONValue(null);
123         case KindCase.nullValue:
124             return JSONValue(null);
125         case KindCase.numberValue:
126             return JSONValue(numberValue);
127         case KindCase.stringValue:
128             return JSONValue(stringValue);
129         case KindCase.boolValue:
130             return JSONValue(boolValue);
131         case KindCase.structValue:
132             return JSONValue(structValue.fields.byKeyValue.map!(a => tuple(a.key, a.value.toJSONValue)).assocArray);
133         case KindCase.listValue:
134             return JSONValue(listValue.values.map!(a => a.toJSONValue).array);
135         }
136     }
137 
138     auto fromJSONValue()(JSONValue jsonValue)
139     {
140         import std.array : array, assocArray;
141         import std.algorithm : map;
142         import std.json : JSON_TYPE;
143         import std.typecons : tuple;
144 
145         switch (jsonValue.type)
146         {
147         case JSON_TYPE.NULL:
148             nullValue = NullValue.NULL_VALUE;
149             break;
150         case JSON_TYPE.STRING:
151             stringValue = jsonValue.str;
152             break;
153         case JSON_TYPE.INTEGER:
154             numberValue = jsonValue.integer;
155             break;
156         case JSON_TYPE.UINTEGER:
157             numberValue = jsonValue.uinteger;
158             break;
159         case JSON_TYPE.FLOAT:
160             numberValue = jsonValue.floating;
161             break;
162         case JSON_TYPE.OBJECT:
163             if (structValue is null)
164                 structValue = new Struct;
165             structValue.fields = jsonValue.object.byKeyValue.map!(a => tuple(a.key, new Value(a.value))).assocArray;
166             break;
167         case JSON_TYPE.ARRAY:
168             if (listValue is null)
169                 listValue = new ListValue;
170             listValue.values = jsonValue.array.map!(a => new Value(a)).array;
171             break;
172         case JSON_TYPE.TRUE:
173             boolValue = true;
174             break;
175         case JSON_TYPE.FALSE:
176             boolValue = false;
177             break;
178         default:
179             throw new ProtobufException("Unexpected JSON type");
180         }
181 
182         return this;
183     }
184 }
185 
186 enum NullValue
187 {
188     NULL_VALUE = 0,
189 }
190 
191 class ListValue
192 {
193     @Proto(1) Value[] values = protoDefaultValue!(Value[]);
194 
195     this()
196     {
197     }
198 
199     this(JSONValue value)
200     {
201         fromJSONValue(value);
202     }
203 
204     JSONValue toJSONValue()()
205     {
206         import std.array : array;
207         import std.algorithm : map;
208 
209         return JSONValue(values.map!(a => a.toJSONValue).array);
210     }
211 
212     auto fromJSONValue()(JSONValue value)
213     {
214         import std.array : array;
215         import std.algorithm : map;
216         import std.exception : enforce;
217         import std.json : JSON_TYPE;
218 
219         if (value.type == JSON_TYPE.NULL)
220         {
221             values = protoDefaultValue!(Value[]);
222             return this;
223         }
224 
225         enforce!ProtobufException(value.type == JSON_TYPE.ARRAY, "JSON array expected");
226         values = value.array.map!(a => new Value(a)).array;
227 
228         return this;
229     }
230 }