| 171 | } |
| 172 | |
| 173 | static bool SetSingleValueForMessage(const Reflection* reflection, |
| 174 | Message* pb, |
| 175 | const FieldDescriptor* field, |
| 176 | const Json::Value& node) { |
| 177 | switch (field->cpp_type()) { |
| 178 | case FieldDescriptor::CPPTYPE_INT32: |
| 179 | reflection->SetInt32(pb, field, node.asInt()); |
| 180 | break; |
| 181 | case FieldDescriptor::CPPTYPE_INT64: { |
| 182 | int64_t number = 0; |
| 183 | if (!toft::StringToNumber(node.asString(), &number)) { |
| 184 | LOG(WARNING) << "Fail to convert to interger:" << node.asString(); |
| 185 | return false; |
| 186 | } |
| 187 | reflection->SetInt64(pb, field, number); |
| 188 | break; |
| 189 | } |
| 190 | case FieldDescriptor::CPPTYPE_UINT32: |
| 191 | reflection->SetUInt32(pb, field, node.asUInt()); |
| 192 | break; |
| 193 | case FieldDescriptor::CPPTYPE_UINT64: { |
| 194 | uint64_t number = 0; |
| 195 | if (!toft::StringToNumber(node.asString(), &number)) { |
| 196 | LOG(WARNING) << "Fail to convert to interger:" << node.asString(); |
| 197 | return false; |
| 198 | } |
| 199 | reflection->SetUInt64(pb, field, number); |
| 200 | break; |
| 201 | } |
| 202 | case FieldDescriptor::CPPTYPE_DOUBLE: |
| 203 | reflection->SetDouble(pb, field, node.asDouble()); |
| 204 | break; |
| 205 | case FieldDescriptor::CPPTYPE_FLOAT: |
| 206 | reflection->SetFloat(pb, field, node.asDouble()); |
| 207 | break; |
| 208 | case FieldDescriptor::CPPTYPE_BOOL: |
| 209 | reflection->SetBool(pb, field, node.asBool()); |
| 210 | break; |
| 211 | case FieldDescriptor::CPPTYPE_ENUM: { |
| 212 | const EnumValueDescriptor* enum_value = reflection->GetEnum(*pb, field); |
| 213 | const EnumDescriptor* enum_desc = enum_value->type(); |
| 214 | const EnumValueDescriptor* real_enum_value = enum_desc->FindValueByNumber(node.asInt()); |
| 215 | reflection->SetEnum(pb, field, real_enum_value); |
| 216 | break; |
| 217 | } |
| 218 | case FieldDescriptor::CPPTYPE_STRING: |
| 219 | reflection->SetString(pb, field, node.asString()); |
| 220 | break; |
| 221 | case FieldDescriptor::CPPTYPE_MESSAGE: { |
| 222 | Message* sub_pb = reflection->MutableMessage(pb, field); |
| 223 | if (!ParseFromJsonValue(node, sub_pb)) { |
| 224 | return false; |
| 225 | } |
| 226 | break; |
| 227 | } |
| 228 | default: |
| 229 | CHECK(false) << "bad type:" << field->cpp_type(); |
| 230 | break; |
no test coverage detected