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 16 #ifndef META_INTERFACE_SERIALIZATION_SER_METADATA_H 17 #define META_INTERFACE_SERIALIZATION_SER_METADATA_H 18 19 #include <meta/base/namespace.h> 20 #include <meta/base/version.h> 21 22 META_BEGIN_NAMESPACE() 23 24 struct SerMetadataEntity { 25 BASE_NS::string key; 26 BASE_NS::string data; 27 }; 28 using SerMetadata = BASE_NS::vector<SerMetadataEntity>; 29 30 struct SerMetadataValues : SerMetadata { 31 SerMetadataValues() = default; SerMetadataValuesSerMetadataValues32 SerMetadataValues(SerMetadata m) : SerMetadata(BASE_NS::move(m)) {} 33 SetSerMetadataValues34 SerMetadataValues& Set(BASE_NS::string_view key, BASE_NS::string_view data) 35 { 36 for (auto&& v : *this) { 37 if (v.key == key) { 38 v.data = data; 39 return *this; 40 } 41 } 42 push_back({ BASE_NS::string(key), BASE_NS::string(data) }); 43 return *this; 44 } GetSerMetadataValues45 BASE_NS::string_view Get(BASE_NS::string_view key) const 46 { 47 for (auto&& v : *this) { 48 if (v.key == key) { 49 return v.data; 50 } 51 } 52 return {}; 53 } SetVersionSerMetadataValues54 SerMetadataValues& SetVersion(const Version& ver) 55 { 56 return Set("version", ver.ToString()); 57 } GetVersionSerMetadataValues58 Version GetVersion() const 59 { 60 auto res = Get("version"); 61 return !res.empty() ? Version(res) : Version(); 62 } SetTypeSerMetadataValues63 SerMetadataValues& SetType(BASE_NS::string_view type) 64 { 65 return Set("type", type); 66 } GetTypeSerMetadataValues67 BASE_NS::string GetType() const 68 { 69 return BASE_NS::string(Get("type")); 70 } 71 }; 72 73 META_END_NAMESPACE() 74 75 #endif 76