proto_builder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "proto_builder.h"
  2. #include <yql/essentials/public/udf/udf_value_builder.h>
  3. #include <util/generic/singleton.h>
  4. using namespace google::protobuf;
  5. namespace {
  6. using namespace NYql::NUdf;
  7. const EnumValueDescriptor& GetEnumValue(const TUnboxedValuePod& source, const FieldDescriptor& field,
  8. const TProtoInfo& info, TFlags<EFieldFlag> flags) {
  9. const auto* enumDescriptor = field.enum_type();
  10. Y_ENSURE(enumDescriptor);
  11. if (flags.HasFlags(EFieldFlag::EnumInt)) {
  12. const auto number = source.Get<i64>();
  13. const auto* result = enumDescriptor->FindValueByNumber(number);
  14. if (!result) {
  15. ythrow yexception() << "unknown value " << number
  16. << " for enum type " << enumDescriptor->full_name()
  17. << ", field " << field.full_name();
  18. }
  19. return *result;
  20. } else if (flags.HasFlags(EFieldFlag::EnumString)) {
  21. const TStringBuf name = source.AsStringRef();
  22. for (int i = 0; i < enumDescriptor->value_count(); ++i) {
  23. const auto& value = *enumDescriptor->value(i);
  24. if (value.name() == name) {
  25. return value;
  26. }
  27. }
  28. ythrow yexception() << "unknown value " << name
  29. << " for enum type " << enumDescriptor->full_name()
  30. << ", field " << field.full_name();
  31. }
  32. if (info.EnumFormat == EEnumFormat::Number) {
  33. const auto number = source.Get<i32>();
  34. const auto* result = enumDescriptor->FindValueByNumber(number);
  35. if (!result) {
  36. ythrow yexception() << "unknown value " << number
  37. << " for enum type " << enumDescriptor->full_name()
  38. << ", field " << field.full_name();
  39. }
  40. return *result;
  41. }
  42. const TStringBuf name = source.AsStringRef();
  43. for (int i = 0; i < enumDescriptor->value_count(); ++i) {
  44. const auto& value = *enumDescriptor->value(i);
  45. const auto& valueName = info.EnumFormat == EEnumFormat::Name ? value.name() : value.full_name();
  46. if (valueName == name) {
  47. return value;
  48. }
  49. }
  50. ythrow yexception() << "unknown value " << name
  51. << " for enum type " << enumDescriptor->full_name()
  52. << ", field " << field.full_name();
  53. }
  54. void FillRepeatedField(const TUnboxedValuePod& source, Message& target,
  55. const FieldDescriptor& field, const TProtoInfo& info, TFlags<EFieldFlag> flags) {
  56. const auto& reflection = *target.GetReflection();
  57. const auto iter = source.GetListIterator();
  58. reflection.ClearField(&target, &field);
  59. for (TUnboxedValue item; iter.Next(item);) {
  60. switch (field.type()) {
  61. case FieldDescriptor::TYPE_DOUBLE:
  62. reflection.AddDouble(&target, &field, item.Get<double>());
  63. break;
  64. case FieldDescriptor::TYPE_FLOAT:
  65. reflection.AddFloat(&target, &field, info.YtMode ? float(item.Get<double>()) : item.Get<float>());
  66. break;
  67. case FieldDescriptor::TYPE_INT64:
  68. case FieldDescriptor::TYPE_SFIXED64:
  69. case FieldDescriptor::TYPE_SINT64:
  70. reflection.AddInt64(&target, &field, item.Get<i64>());
  71. break;
  72. case FieldDescriptor::TYPE_ENUM:
  73. {
  74. const auto& enumValue = GetEnumValue(item, field, info, flags);
  75. reflection.AddEnum(&target, &field, &enumValue);
  76. }
  77. break;
  78. case FieldDescriptor::TYPE_UINT64:
  79. case FieldDescriptor::TYPE_FIXED64:
  80. reflection.AddUInt64(&target, &field, item.Get<ui64>());
  81. break;
  82. case FieldDescriptor::TYPE_INT32:
  83. case FieldDescriptor::TYPE_SFIXED32:
  84. case FieldDescriptor::TYPE_SINT32:
  85. reflection.AddInt32(&target, &field, item.Get<i32>());
  86. break;
  87. case FieldDescriptor::TYPE_UINT32:
  88. case FieldDescriptor::TYPE_FIXED32:
  89. reflection.AddUInt32(&target, &field, item.Get<ui32>());
  90. break;
  91. case FieldDescriptor::TYPE_BOOL:
  92. reflection.AddBool(&target, &field, item.Get<bool>());
  93. break;
  94. case FieldDescriptor::TYPE_STRING:
  95. reflection.AddString(&target, &field, TString(item.AsStringRef()));
  96. break;
  97. case FieldDescriptor::TYPE_BYTES:
  98. reflection.AddString(&target, &field, TString(item.AsStringRef()));
  99. break;
  100. case FieldDescriptor::TYPE_MESSAGE:
  101. {
  102. auto* nestedMessage = reflection.AddMessage(&target, &field);
  103. if (flags.HasFlags(EFieldFlag::Binary)) {
  104. const auto& bytes = TStringBuf(item.AsStringRef());
  105. Y_ENSURE(nestedMessage->ParseFromArray(bytes.data(), bytes.size()));
  106. } else {
  107. FillProtoFromValue(item, *nestedMessage, info);
  108. }
  109. }
  110. break;
  111. default:
  112. ythrow yexception() << "Unsupported protobuf type: "
  113. << field.type_name() << ", field: " << field.name();
  114. }
  115. }
  116. }
  117. void FillSingleField(const TUnboxedValuePod& source, Message& target,
  118. const FieldDescriptor& field, const TProtoInfo& info, TFlags<EFieldFlag> flags) {
  119. const auto& reflection = *target.GetReflection();
  120. switch (field.type()) {
  121. case FieldDescriptor::TYPE_DOUBLE:
  122. reflection.SetDouble(&target, &field, source.Get<double>());
  123. break;
  124. case FieldDescriptor::TYPE_FLOAT:
  125. reflection.SetFloat(&target, &field, info.YtMode ? float(source.Get<double>()) : source.Get<float>());
  126. break;
  127. case FieldDescriptor::TYPE_INT64:
  128. case FieldDescriptor::TYPE_SFIXED64:
  129. case FieldDescriptor::TYPE_SINT64:
  130. reflection.SetInt64(&target, &field, source.Get<i64>());
  131. break;
  132. case FieldDescriptor::TYPE_ENUM:
  133. {
  134. const auto& enumValue = GetEnumValue(source, field, info, flags);
  135. reflection.SetEnum(&target, &field, &enumValue);
  136. }
  137. break;
  138. case FieldDescriptor::TYPE_UINT64:
  139. case FieldDescriptor::TYPE_FIXED64:
  140. reflection.SetUInt64(&target, &field, source.Get<ui64>());
  141. break;
  142. case FieldDescriptor::TYPE_INT32:
  143. case FieldDescriptor::TYPE_SFIXED32:
  144. case FieldDescriptor::TYPE_SINT32:
  145. reflection.SetInt32(&target, &field, source.Get<i32>());
  146. break;
  147. case FieldDescriptor::TYPE_UINT32:
  148. case FieldDescriptor::TYPE_FIXED32:
  149. reflection.SetUInt32(&target, &field, source.Get<ui32>());
  150. break;
  151. case FieldDescriptor::TYPE_BOOL:
  152. reflection.SetBool(&target, &field, source.Get<bool>());
  153. break;
  154. case FieldDescriptor::TYPE_STRING:
  155. reflection.SetString(&target, &field, TString(source.AsStringRef()));
  156. break;
  157. case FieldDescriptor::TYPE_BYTES:
  158. reflection.SetString(&target, &field, TString(source.AsStringRef()));
  159. break;
  160. case FieldDescriptor::TYPE_MESSAGE:
  161. {
  162. auto* nestedMessage = reflection.MutableMessage(&target, &field);
  163. if (flags.HasFlags(EFieldFlag::Binary)) {
  164. const auto& bytes = TStringBuf(source.AsStringRef());
  165. Y_ENSURE(nestedMessage->ParseFromArray(bytes.data(), bytes.size()));
  166. } else {
  167. FillProtoFromValue(source, *nestedMessage, info);
  168. }
  169. }
  170. break;
  171. default:
  172. ythrow yexception() << "Unsupported protobuf type: "
  173. << field.type_name() << ", field: " << field.name();
  174. }
  175. }
  176. void FillMapField(const TUnboxedValuePod& source, Message& target, const FieldDescriptor& field, const TProtoInfo& info, TFlags<EFieldFlag> flags) {
  177. const auto& reflection = *target.GetReflection();
  178. reflection.ClearField(&target, &field);
  179. if (source) {
  180. const auto noBinaryFlags = TFlags<EFieldFlag>(flags).RemoveFlags(EFieldFlag::Binary);
  181. const auto iter = source.GetDictIterator();
  182. for (TUnboxedValue key, value; iter.NextPair(key, value);) {
  183. auto* nestedMessage = reflection.AddMessage(&target, &field);
  184. const auto& descriptor = *nestedMessage->GetDescriptor();
  185. FillSingleField(key, *nestedMessage, *descriptor.map_key(), info, noBinaryFlags);
  186. FillSingleField(value, *nestedMessage, *descriptor.map_value(), info, flags);
  187. }
  188. }
  189. }
  190. }
  191. namespace NYql::NUdf {
  192. void FillProtoFromValue(const TUnboxedValuePod& source, Message& target, const TProtoInfo& info) {
  193. const auto& descriptor = *target.GetDescriptor();
  194. TMessageInfo* messageInfo;
  195. {
  196. const auto it = info.Messages.find(descriptor.full_name());
  197. if (it == info.Messages.end()) {
  198. ythrow yexception() << "unknown message " << descriptor.full_name();
  199. }
  200. messageInfo = it->second.get();
  201. }
  202. const auto& reflection = *target.GetReflection();
  203. for (int i = 0; i < descriptor.field_count(); ++i) {
  204. const auto& field = *descriptor.field(i);
  205. const auto it = messageInfo->Fields.find(field.number());
  206. Y_ENSURE(it != messageInfo->Fields.end());
  207. auto pos = it->second.Pos;
  208. TFlags<EFieldFlag> flags = it->second.Flags;
  209. auto fieldValue = source.GetElement(pos);
  210. if (field.containing_oneof() && flags.HasFlags(EFieldFlag::Variant)) {
  211. const ui32* varIndex = messageInfo->VariantIndicies.FindPtr(field.number());
  212. Y_ENSURE(varIndex);
  213. if (fieldValue && fieldValue.GetVariantIndex() == *varIndex) {
  214. fieldValue = fieldValue.GetVariantItem();
  215. } else {
  216. reflection.ClearField(&target, &field);
  217. continue;
  218. }
  219. }
  220. if (flags.HasFlags(EFieldFlag::Void)) {
  221. reflection.ClearField(&target, &field);
  222. continue;
  223. }
  224. if (!fieldValue) {
  225. if (field.is_required()) {
  226. ythrow yexception() << "required field " << field.name() << " has no value";
  227. }
  228. reflection.ClearField(&target, &field);
  229. continue;
  230. }
  231. if (field.is_map() && flags.HasFlags(EFieldFlag::Dict)) {
  232. FillMapField(fieldValue, target, field, info, flags);
  233. } else if (field.is_repeated()) {
  234. FillRepeatedField(fieldValue, target, field, info, flags);
  235. } else {
  236. FillSingleField(fieldValue, target, field, info, flags);
  237. }
  238. }
  239. }
  240. } // namespace NYql::NUdf