• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef SCHEMA_UTILS_H
17 #define SCHEMA_UTILS_H
18 
19 #include "db_types.h"
20 #include "schema_object.h"
21 
22 // This header is supposed to be included only in source files. Do not include it in any header files.
23 namespace DistributedDB {
24 class SchemaUtils {
25 public:
26     // Check if any invalid exist, parse it into SchemaAttribute if totally valid and return E_OK
27     // Number don't support format of scientific notation. SchemaAttribute.isIndexable always set true.
28     // Prefix and postfix spaces or tabs is allowed.
29     // Customer FieldType will be accepted if useAffinity is true.
30     static int ParseAndCheckSchemaAttribute(const std::string &inAttrString, SchemaAttribute &outAttr,
31         bool useAffinity = false);
32 
33     // Check if any invalid exist, parse it into FieldPath if totally valid and return E_OK
34     // Each fieldName of the fieldPath will be check valid as well. Path depth will be check.
35     // Prefix and postfix spaces or tabs is allowed. Prefix $. can be not exist.
36     // Parameter permitPrefix means whether $. prefix is permited. If not, return E_SCHEMA_PARSE_FAIL.
37     static int ParseAndCheckFieldPath(const std::string &inPathString, FieldPath &outPath, bool permitPrefix = true);
38 
39     // Return E_OK if it is totally valid. Prefix and postfix spaces or tabs is not allowed.
40     static int CheckFieldName(const FieldName &inName);
41 
42     // Remove prefix and postfix spaces or tabs
43     static std::string Strip(const std::string &inString);
44 
45     // Strip the namespace from the full-name, this method mainly for flatbuffer-type schema
46     static std::string StripNameSpace(const std::string &inFullName);
47 
48     static std::string FieldTypeString(FieldType inType);
49     static std::string SchemaTypeString(SchemaType inType);
50 
51     // Restore to string representation of fieldPath with $. prefix
52     static std::string FieldPathString(const FieldPath &inPath);
53 
54     static void TransTrackerSchemaToLower(const TrackerSchema &srcSchema, TrackerSchema &destSchema);
55 
56     static int ExtractJsonObj(const JsonObject &inJsonObject, const std::string &field, JsonObject &out);
57 
58     static int ExtractJsonObjArray(const JsonObject &inJsonObject, const std::string &field,
59         std::vector<JsonObject> &out);
60 
61     SchemaUtils() = delete;
62     ~SchemaUtils() = delete;
63 
64 private:
65 
66     static int SplitSchemaAttribute(const std::string &inAttrString, std::vector<std::string> &outAttrString);
67     static int MakeTrans(const std::string &oriContent, size_t &pos);
68 
69     static int ParseSchemaAttribute(std::vector<std::string> &attrContext, SchemaAttribute &outAttr, bool useAffinity);
70 
71     static int TransformDefaultValue(std::string &defaultContent, SchemaAttribute &outAttr);
72 
73     static int TransToDouble(const std::string &defaultContent, SchemaAttribute &outAttr);
74 
75     static int TransToInteger(const std::string &defaultContent, SchemaAttribute &outAttr);
76 
77     static int TransToLong(const std::string &defaultContent, SchemaAttribute &outAttr);
78 
79     static int TransToString(const std::string &defaultContent, SchemaAttribute &outAttr);
80 
81     static int TransToBool(const std::string &defaultContent, SchemaAttribute &outAttr);
82 };
83 } // namespace DistributedDB
84 #endif // SCHEMA_UTILS_H