1515#include " google/cloud/bigquery/v2/minimal/internal/common_v2_resources.h"
1616#include " google/cloud/internal/debug_string.h"
1717#include " google/cloud/internal/format_time_point.h"
18+ #include " google/cloud/log.h"
1819
1920namespace google {
2021namespace cloud {
@@ -79,6 +80,176 @@ void from_json(nlohmann::json const& j, QueryParameterValue& q) {
7980 if (j.contains (" struct_values" ))
8081 j.at (" struct_values" ).get_to (q.struct_values );
8182}
83+
84+ void to_json (nlohmann::json& j, StandardSqlField const & f) {
85+ if (f.type != nullptr ) {
86+ j = nlohmann::json{{" name" , f.name }, {" type" , *f.type }};
87+ } else {
88+ j = nlohmann::json{{" name" , f.name }};
89+ }
90+ }
91+
92+ void from_json (nlohmann::json const & j, StandardSqlField& f) {
93+ if (j.contains (" name" )) j.at (" name" ).get_to (f.name );
94+ if (f.type == nullptr ) {
95+ f.type = std::make_shared<StandardSqlDataType>();
96+ }
97+ if (j.contains (" type" )) j.at (" type" ).get_to (*f.type );
98+ }
99+
100+ void to_json (nlohmann::json& j, StandardSqlStructType const & t) {
101+ j = nlohmann::json{{" fields" , t.fields }};
102+ }
103+
104+ void from_json (nlohmann::json const & j, StandardSqlStructType& t) {
105+ if (j.contains (" fields" )) j.at (" fields" ).get_to (t.fields );
106+ }
107+
108+ void to_json (nlohmann::json& j, StandardSqlDataType const & t) {
109+ if (t.sub_type .valueless_by_exception ()) {
110+ j = nlohmann::json{{" type_kind" , t.type_kind }};
111+ return ;
112+ }
113+
114+ struct Visitor {
115+ TypeKind type_kind;
116+ nlohmann::json& j;
117+
118+ void operator ()(absl::monostate const &) {
119+ j = nlohmann::json{{" type_kind" , std::move (type_kind)}};
120+ }
121+ void operator ()(std::shared_ptr<StandardSqlDataType> const & type) {
122+ j = nlohmann::json{{" type_kind" , std::move (type_kind)},
123+ {" sub_type" , *type},
124+ {" sub_type_index" , 1 }};
125+ }
126+ void operator ()(StandardSqlStructType const & type) {
127+ j = nlohmann::json{{" type_kind" , std::move (type_kind)},
128+ {" sub_type" , type},
129+ {" sub_type_index" , 2 }};
130+ }
131+ };
132+
133+ absl::visit (Visitor{t.type_kind , j}, t.sub_type );
134+ }
135+
136+ void from_json (nlohmann::json const & j, StandardSqlDataType& t) {
137+ if (j.contains (" type_kind" )) j.at (" type_kind" ).get_to (t.type_kind );
138+ if (j.contains (" sub_type_index" ) && j.contains (" sub_type" )) {
139+ auto const index = j.at (" sub_type_index" ).get <int >();
140+ switch (index) {
141+ case 1 :
142+ t.sub_type = std::make_shared<StandardSqlDataType>(
143+ j.at (" sub_type" ).get <StandardSqlDataType>());
144+ break ;
145+ case 2 :
146+ t.sub_type = j.at (" sub_type" ).get <StandardSqlStructType>();
147+ break ;
148+ default :
149+ break ;
150+ }
151+ }
152+ }
153+
154+ void to_json (nlohmann::json& j, Value const & v) {
155+ if (v.value_kind .valueless_by_exception ()) {
156+ return ;
157+ }
158+
159+ struct Visitor {
160+ nlohmann::json& j;
161+
162+ void operator ()(absl::monostate const &) {
163+ // Nothing to do.
164+ }
165+ void operator ()(double const & val) {
166+ j = nlohmann::json{{" value_kind" , val}, {" kind_index" , 1 }};
167+ }
168+ void operator ()(std::string const & val) {
169+ j = nlohmann::json{{" value_kind" , val}, {" kind_index" , 2 }};
170+ }
171+ void operator ()(bool const & val) {
172+ j = nlohmann::json{{" value_kind" , val}, {" kind_index" , 3 }};
173+ }
174+ void operator ()(std::shared_ptr<Struct> const & val) {
175+ j = nlohmann::json{{" value_kind" , *val}, {" kind_index" , 4 }};
176+ }
177+ void operator ()(std::vector<Value> const & val) {
178+ j = nlohmann::json{{" value_kind" , val}, {" kind_index" , 5 }};
179+ }
180+ };
181+
182+ absl::visit (Visitor{j}, v.value_kind );
183+ }
184+
185+ void from_json (nlohmann::json const & j, Value& v) {
186+ if (j.contains (" kind_index" ) && j.contains (" value_kind" )) {
187+ auto const index = j.at (" kind_index" ).get <int >();
188+ switch (index) {
189+ case 0 :
190+ // Do not set any value
191+ break ;
192+ case 1 :
193+ v.value_kind = j.at (" value_kind" ).get <double >();
194+ break ;
195+ case 2 :
196+ v.value_kind = j.at (" value_kind" ).get <std::string>();
197+ break ;
198+ case 3 :
199+ v.value_kind = j.at (" value_kind" ).get <bool >();
200+ break ;
201+ case 4 :
202+ v.value_kind =
203+ std::make_shared<Struct>(j.at (" value_kind" ).get <Struct>());
204+ break ;
205+ case 5 :
206+ v.value_kind = j.at (" value_kind" ).get <std::vector<Value>>();
207+ break ;
208+ default :
209+ GCP_LOG (FATAL) << " Invalid kind_index for Value: " << index;
210+ break ;
211+ }
212+ }
213+ }
214+
215+ void to_json (nlohmann::json& j, Struct const & s) {
216+ j = nlohmann::json{{" fields" , s.fields }};
217+ }
218+
219+ void from_json (nlohmann::json const & j, Struct& s) {
220+ if (j.contains (" fields" )) j.at (" fields" ).get_to (s.fields );
221+ }
222+
223+ void to_json (nlohmann::json& j, SystemVariables const & s) {
224+ j = nlohmann::json{{" types" , s.types }, {" values" , s.values }};
225+ }
226+
227+ void from_json (nlohmann::json const & j, SystemVariables& s) {
228+ if (j.contains (" types" )) j.at (" types" ).get_to (s.types );
229+ if (j.contains (" values" )) j.at (" values" ).get_to (s.values );
230+ }
231+
232+ bool operator ==(StandardSqlStructType const & lhs,
233+ StandardSqlStructType const & rhs) {
234+ return std::equal (lhs.fields .begin (), lhs.fields .end (), rhs.fields .begin ());
235+ }
236+
237+ bool operator ==(StandardSqlField const & lhs, StandardSqlField const & rhs) {
238+ auto const eq_name = (lhs.name == rhs.name );
239+ if (lhs.type != nullptr && rhs.type != nullptr ) {
240+ return eq_name && (*lhs.type == *rhs.type );
241+ }
242+ return eq_name && lhs.type == nullptr && rhs.type == nullptr ;
243+ }
244+
245+ bool operator ==(StandardSqlDataType const & lhs,
246+ StandardSqlDataType const & rhs) {
247+ return (lhs.type_kind .value == rhs.type_kind .value );
248+ }
249+
250+ bool operator ==(Value const & lhs, Value const & rhs) {
251+ return (lhs.value_kind == rhs.value_kind );
252+ }
82253// NOLINTEND
83254
84255RoundingMode RoundingMode::UnSpecified () {
@@ -93,6 +264,52 @@ RoundingMode RoundingMode::RoundHalfEven() {
93264 return RoundingMode{" ROUND_HALF_EVEN" };
94265}
95266
267+ KeyResultStatementKind KeyResultStatementKind::UnSpecified () {
268+ return KeyResultStatementKind{" KEY_RESULT_STATEMENT_KIND_UNSPECIFIED" };
269+ }
270+
271+ KeyResultStatementKind KeyResultStatementKind::Last () {
272+ return KeyResultStatementKind{" LAST" };
273+ }
274+
275+ KeyResultStatementKind KeyResultStatementKind::FirstSelect () {
276+ return KeyResultStatementKind{" FIRST_SELECT" };
277+ }
278+
279+ TypeKind TypeKind::UnSpecified () { return TypeKind{" TYPE_KIND_UNSPECIFIED" }; }
280+
281+ TypeKind TypeKind::Int64 () { return TypeKind{" INT64" }; }
282+
283+ TypeKind TypeKind::Bool () { return TypeKind{" BOOL" }; }
284+
285+ TypeKind TypeKind::Float64 () { return TypeKind{" FLOAT64" }; }
286+
287+ TypeKind TypeKind::String () { return TypeKind{" STRING" }; }
288+
289+ TypeKind TypeKind::Bytes () { return TypeKind{" BYTES" }; }
290+
291+ TypeKind TypeKind::Timestamp () { return TypeKind{" TIMESTAMP" }; }
292+
293+ TypeKind TypeKind::Date () { return TypeKind{" DATE" }; }
294+
295+ TypeKind TypeKind::Time () { return TypeKind{" TIME" }; }
296+
297+ TypeKind TypeKind::DateTime () { return TypeKind{" DATETIME" }; }
298+
299+ TypeKind TypeKind::Interval () { return TypeKind{" INTERVAL" }; }
300+
301+ TypeKind TypeKind::Geography () { return TypeKind{" GEOGRAPHY" }; }
302+
303+ TypeKind TypeKind::Numeric () { return TypeKind{" NUMERIC" }; }
304+
305+ TypeKind TypeKind::BigNumeric () { return TypeKind{" BIGNUMERIC" }; }
306+
307+ TypeKind TypeKind::Json () { return TypeKind{" JSON" }; }
308+
309+ TypeKind TypeKind::Array () { return TypeKind{" ARRAY" }; }
310+
311+ TypeKind TypeKind::Struct () { return TypeKind{" STRUCT" }; }
312+
96313std::string ErrorProto::DebugString (absl::string_view name,
97314 TracingOptions const & options,
98315 int indent) const {
0 commit comments