1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "schema_utils.h"
17 namespace DistributedDB {
TrimFiled(std::string & inString)18 void SchemaUtils::TrimFiled(std::string &inString)
19 {
20 inString.erase(0, inString.find_first_not_of("\r\t "));
21 size_t temp = inString.find_last_not_of("\r\t ");
22 if (temp < inString.size()) {
23 inString.erase(temp + 1);
24 }
25 }
26
Strip(const std::string & inString)27 std::string SchemaUtils::Strip(const std::string &inString)
28 {
29 std::string stripRes = inString;
30 TrimFiled(stripRes);
31 return stripRes;
32 }
33
FieldTypeString(FieldType inType)34 std::string SchemaUtils::FieldTypeString(FieldType inType)
35 {
36 static std::map<FieldType, std::string> fieldTypeMapString = {
37 {FieldType::LEAF_FIELD_NULL, "NULL"},
38 {FieldType::LEAF_FIELD_BOOL, "BOOL"},
39 {FieldType::LEAF_FIELD_INTEGER, "INTEGER"},
40 {FieldType::LEAF_FIELD_LONG, "LONG"},
41 {FieldType::LEAF_FIELD_DOUBLE, "DOUBLE"},
42 {FieldType::LEAF_FIELD_STRING, "STRING"},
43 {FieldType::LEAF_FIELD_ARRAY, "ARRAY"},
44 {FieldType::LEAF_FIELD_OBJECT, "LEAF_OBJECT"},
45 {FieldType::INTERNAL_FIELD_OBJECT, "INTERNAL_OBJECT"},
46 };
47 return fieldTypeMapString[inType];
48 }
49
ExtractJsonObj(const JsonObject & inJsonObject,const std::string & field,JsonObject & out)50 int SchemaUtils::ExtractJsonObj(const JsonObject &inJsonObject, const std::string &field,
51 JsonObject &out)
52 {
53 FieldType fieldType;
54 auto fieldPath = FieldPath {field};
55 int errCode = inJsonObject.GetFieldTypeByFieldPath(fieldPath, fieldType);
56 if (errCode != E_OK) {
57 LOGE("[SchemaUtils][ExtractJsonObj] Get schema fieldType failed: %d.", errCode);
58 return -E_SCHEMA_PARSE_FAIL;
59 }
60 if (FieldType::INTERNAL_FIELD_OBJECT != fieldType) {
61 LOGE("[SchemaUtils][ExtractJsonObj] Expect Object but %s.",
62 SchemaUtils::FieldTypeString(fieldType).c_str());
63 return -E_SCHEMA_PARSE_FAIL;
64 }
65 errCode = inJsonObject.GetObjectByFieldPath(fieldPath, out);
66 if (errCode != E_OK) {
67 LOGE("[SchemaUtils][ExtractJsonObj] Get schema value failed: %d.", errCode);
68 return -E_SCHEMA_PARSE_FAIL;
69 }
70 return E_OK;
71 }
72
ExtractJsonObjArray(const JsonObject & inJsonObject,const std::string & field,std::vector<JsonObject> & out)73 int SchemaUtils::ExtractJsonObjArray(const JsonObject &inJsonObject, const std::string &field,
74 std::vector<JsonObject> &out)
75 {
76 FieldType fieldType;
77 auto fieldPath = FieldPath {field};
78 int errCode = inJsonObject.GetFieldTypeByFieldPath(fieldPath, fieldType);
79 if (errCode != E_OK) {
80 LOGE("[SchemaUtils][ExtractJsonObjArray] Get schema fieldType failed: %d.", errCode);
81 return -E_SCHEMA_PARSE_FAIL;
82 }
83 if (FieldType::LEAF_FIELD_ARRAY != fieldType) {
84 LOGE("[SchemaUtils][ExtractJsonObjArray] Expect Array but %s.",
85 SchemaUtils::FieldTypeString(fieldType).c_str());
86 return -E_SCHEMA_PARSE_FAIL;
87 }
88 errCode = inJsonObject.GetObjectArrayByFieldPath(fieldPath, out);
89 if (errCode != E_OK) {
90 LOGE("[SchemaUtils][ExtractJsonObjArray] Get schema value failed: %d.", errCode);
91 return -E_SCHEMA_PARSE_FAIL;
92 }
93 return E_OK;
94 }
95 } // namespace DistributedDB
96