• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #ifndef OHOS_MEDIA_CLOUD_SYNC_RECORD_FIELD_H
16 #define OHOS_MEDIA_CLOUD_SYNC_RECORD_FIELD_H
17 #include <map>
18 #include <string>
19 #include <variant>
20 #include <vector>
21 
22 #include "mdk_asset.h"
23 #include "mdk_error.h"
24 #include "mdk_reference.h"
25 #include "json/json.h"
26 
27 #define EXPORT __attribute__ ((visibility ("default")))
28 
29 namespace OHOS::Media::CloudSync {
30 // 记录中值的类型
31 enum class MDKRecordFieldType {
32     FIELD_TYPE_NULL = 0,
33     FIELD_TYPE_INT,        // int64_t
34     FIELD_TYPE_DOUBLE,     // double
35     FIELD_TYPE_STRING,     // std::string
36     FIELD_TYPE_BOOL,       // bool
37     FIELD_TYPE_BLOB,       // std::vector<uint8_t>
38     FIELD_TYPE_LIST,       // std::vector<MDKRecordField>
39     FIELD_TYPE_MAP,        // std::map<std::string, MDKRecordField>
40     FIELD_TYPE_ASSET,      // MDKAsset
41     FIELD_TYPE_REFERENCE,  // MDKReference
42 };
43 struct MDKSchemaField;
44 class MDKRecordField;
45 using MDKFieldValue = std::variant<std::monostate, int64_t, double, std::string, bool, std::vector<uint8_t>,
46     std::vector<MDKRecordField>, std::map<std::string, MDKRecordField>, MDKAsset, MDKReference>;
47 // MDKRecordField用来保存记录中的值
48 
49 class EXPORT MDKRecordField {
50 public:
51     MDKRecordField();
52     ~MDKRecordField();
53     MDKRecordField(MDKFieldValue fieldValue) noexcept;
54     MDKRecordField(const MDKRecordField &recordField);
55     explicit MDKRecordField(int val);
56     explicit MDKRecordField(int64_t val);
57     explicit MDKRecordField(double val);
58     explicit MDKRecordField(bool val);
59     explicit MDKRecordField(const char *val);
60     explicit MDKRecordField(const std::string &val);
61     explicit MDKRecordField(const std::vector<uint8_t> &val);
62     explicit MDKRecordField(std::map<std::string, MDKRecordField> &val);
63     explicit MDKRecordField(std::vector<MDKRecordField> &val);
64     explicit MDKRecordField(MDKAsset &val);
65     explicit MDKRecordField(MDKReference &val);
66     MDKRecordField &operator=(const MDKRecordField &recordField);
67     MDKRecordFieldType GetType() const;
68     MDKFieldValue GetFieldValue() const;
69     MDKLocalErrorCode GetInt(int &val) const;
70     MDKLocalErrorCode GetLong(int64_t &val) const;
71     MDKLocalErrorCode GetDouble(double &val) const;
72     MDKLocalErrorCode GetBool(bool &val) const;
73     MDKLocalErrorCode GetString(std::string &val) const;
74     MDKLocalErrorCode GetBlob(std::vector<uint8_t> &val) const;
75     MDKLocalErrorCode GetRecordList(std::vector<MDKRecordField> &val) const;
76     MDKLocalErrorCode GetRecordMap(std::map<std::string, MDKRecordField> &val) const;
77     MDKLocalErrorCode GetAsset(MDKAsset &val) const;
78     MDKLocalErrorCode GetReference(MDKReference &val) const;
79 
80     operator int() const
81     {
82         return static_cast<int>(int64_t(std::get<int64_t>(value_)));
83     }
int64_t()84     operator int64_t() const
85     {
86         return std::get<int64_t>(value_);
87     }
88     operator double() const
89     {
90         return std::get<double>(value_);
91     }
92     operator bool() const
93     {
94         return std::get<bool>(value_);
95     }
string()96     operator std::string() const
97     {
98         return std::get<std::string>(value_);
99     }
100     operator std::vector<uint8_t>() const
101     {
102         return std::get<std::vector<uint8_t>>(value_);
103     }
104     operator std::vector<MDKRecordField>() const
105     {
106         return std::get<std::vector<MDKRecordField>>(value_);
107     }
108     operator std::map<std::string, MDKRecordField>() const
109     {
110         return std::get<std::map<std::string, MDKRecordField>>(value_);
111     }
MDKAsset()112     operator MDKAsset() const
113     {
114         return std::get<MDKAsset>(value_);
115     }
MDKReference()116     operator MDKReference() const
117     {
118         return std::get<MDKReference>(value_);
119     }
MDKFieldValue()120     operator MDKFieldValue() const
121     {
122         return value_;
123     }
124     Json::Value ToJsonValue();
125     bool ParseFromJsonValue(const MDKSchemaField &schemaField, const Json::Value &jvData);
126 
127 private:
128     Json::Value FieldListToJsonValue();
129     Json::Value FieldMapToJsonValue();
130     Json::Value AssetToJsonValue(const MDKAsset &asset);
131     Json::Value FieldAssetToJsonValue();
132     Json::Value FieldReferenceToJsonValue();
133     bool ParseIntFromJson(const Json::Value &jvData);
134     bool ParseDoubleFromJson(const Json::Value &jvData);
135     bool ParseStringFromJson(const Json::Value &jvData);
136     bool ParseBoolFromJson(const Json::Value &jvData);
137     bool ParseBlobFromJson(const Json::Value &jvData);
138     bool ParseListFromJson(MDKRecordFieldType listType, const Json::Value &jvData);
139     bool ParseMapFromJson(const Json::Value &jvData);
140     bool ParseAssetFromJson(const Json::Value &jvData);
141     bool ParseReferenceFromJson(const Json::Value &jvData);
142 
143     MDKAsset ParseAssetFromJsonValue(const Json::Value &jvData);
144 
145 private:
146     MDKRecordFieldType type_;
147     MDKFieldValue value_;
148 };
149 }  // namespace OHOS::Media::CloudSync
150 #endif