Skip to content

Commit 4e90a2b

Browse files
authored
Add a new library ini_config that only works with ini format (#20810)
1 parent f3bd021 commit 4e90a2b

File tree

12 files changed

+842
-5
lines changed

12 files changed

+842
-5
lines changed

ydb/public/lib/ydb_cli/common/aws.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const TString TCommandWithAwsCredentials::AwsCredentialsFile = "~/.aws/credentia
1515
const TString TCommandWithAwsCredentials::AwsDefaultProfileName = "default";
1616

1717
TString TCommandWithAwsCredentials::ReadIniKey(const TString& iniKey) {
18-
using namespace NConfig;
18+
using namespace NIniConfig;
1919

2020
const auto fileName = "AWS Credentials";
2121
const auto& profileName = AwsProfile.GetOrElse(AwsDefaultProfileName);

ydb/public/lib/ydb_cli/common/aws.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

33
#include "command.h"
4-
5-
#include <library/cpp/config/config.h>
4+
#include "ini_config/config.h"
65

76
#include <util/generic/maybe.h>
87
#include <util/system/env.h>
@@ -60,7 +59,7 @@ class TCommandWithAwsCredentials {
6059
static const TString AwsDefaultProfileName;
6160

6261
private:
63-
TMaybe<NConfig::TConfig> Config;
62+
TMaybe<NIniConfig::TConfig> Config;
6463
TMaybe<TString> AwsProfile;
6564
};
6665

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "config.h"
2+
#include "ini.h"
3+
4+
#include <util/stream/mem.h>
5+
6+
namespace NIniConfig {
7+
8+
TConfig TConfig::ReadIni(TStringBuf in) {
9+
TMemoryInput mi(in.data(), in.size());
10+
11+
return ParseIni(mi);
12+
}
13+
14+
bool TConfig::Has(const TStringBuf& key) const {
15+
return !operator[](key).IsNull();
16+
}
17+
18+
const TConfig& TConfig::operator[](const TStringBuf& key) const {
19+
return Get<TDict>().Find(key);
20+
}
21+
22+
const TConfig& TConfig::At(const TStringBuf& key) const {
23+
return Get<TDict>().At(key);
24+
}
25+
26+
const TConfig& TConfig::operator[](size_t index) const {
27+
return Get<TArray>().Index(index);
28+
}
29+
30+
size_t TConfig::GetArraySize() const {
31+
return Get<TArray>().size();
32+
}
33+
34+
const TConfig& TDict::Find(const TStringBuf& key) const {
35+
const_iterator it = find(key);
36+
37+
if (it == end()) {
38+
return Default<TConfig>();
39+
}
40+
41+
return it->second;
42+
}
43+
44+
const TConfig& TDict::At(const TStringBuf& key) const {
45+
const_iterator it = find(key);
46+
47+
Y_ENSURE_BT(it != end(), "missing key '" << key << "'");
48+
49+
return it->second;
50+
}
51+
52+
const TConfig& TArray::Index(size_t index) const {
53+
if (index < size()) {
54+
return (*this)[index];
55+
}
56+
57+
return Default<TConfig>();
58+
}
59+
60+
const TConfig& TArray::At(size_t index) const {
61+
Y_ENSURE_BT(index < size(), "index " << index << " is out of bounds");
62+
63+
return (*this)[index];
64+
}
65+
66+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#pragma once
2+
3+
#include "value.h"
4+
5+
#include <util/generic/hash.h>
6+
#include <util/generic/ptr.h>
7+
#include <util/generic/deque.h>
8+
#include <util/generic/string.h>
9+
#include <util/generic/vector.h>
10+
#include <util/generic/yexception.h>
11+
#include <util/stream/input.h>
12+
13+
namespace NIniConfig {
14+
class TConfigError: public TWithBackTrace<yexception> {
15+
};
16+
17+
class TConfigParseError: public TConfigError {
18+
};
19+
20+
class TTypeMismatch: public TConfigError {
21+
};
22+
23+
struct TArray;
24+
struct TDict;
25+
26+
class TConfig {
27+
public:
28+
inline TConfig()
29+
: V_(Null())
30+
{
31+
}
32+
33+
inline TConfig(IValue* v)
34+
: V_(v)
35+
{
36+
}
37+
38+
TConfig(const TConfig& config) = default;
39+
TConfig& operator=(const TConfig& config) = default;
40+
41+
template <class T>
42+
inline bool IsA() const {
43+
return V_->IsA(typeid(T));
44+
}
45+
46+
inline bool IsNumeric() const {
47+
return IsA<double>() || IsA<i64>() || IsA<ui64>();
48+
}
49+
50+
template <class T>
51+
inline const T& Get() const {
52+
return GetNonConstant<T>();
53+
}
54+
55+
template <class T>
56+
inline T& GetNonConstant() const {
57+
if (this->IsA<T>()) {
58+
return *(T*)V_->Ptr();
59+
}
60+
61+
if constexpr (std::is_same_v<T, ::NIniConfig::TArray>) {
62+
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), "array");
63+
} else if constexpr (std::is_same_v<T, ::NIniConfig::TDict>) {
64+
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), "dict");
65+
} else if constexpr (std::is_same_v<T, TString>) {
66+
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), "string");
67+
} else {
68+
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), ::TypeName<T>());
69+
}
70+
}
71+
72+
template <class T>
73+
inline T As() const {
74+
return ValueAs<T>(V_.Get());
75+
}
76+
77+
template <class T>
78+
inline T As(T def) const {
79+
return IsNull() ? def : As<T>();
80+
}
81+
82+
inline bool IsNull() const noexcept {
83+
return V_.Get() == Null();
84+
}
85+
86+
const TConfig& Or(const TConfig& r) const {
87+
return IsNull() ? r : *this;
88+
}
89+
90+
//assume value is dict
91+
bool Has(const TStringBuf& key) const;
92+
const TConfig& operator[](const TStringBuf& key) const;
93+
const TConfig& At(const TStringBuf& key) const;
94+
95+
//assume value is array
96+
const TConfig& operator[](size_t index) const;
97+
size_t GetArraySize() const;
98+
99+
static TConfig ReadIni(TStringBuf content);
100+
101+
private:
102+
TIntrusivePtr<IValue> V_;
103+
};
104+
105+
struct TArray: public TDeque<TConfig> {
106+
const TConfig& Index(size_t index) const;
107+
const TConfig& At(size_t index) const;
108+
};
109+
110+
struct TDict: public THashMap<TString, TConfig> {
111+
const TConfig& Find(const TStringBuf& key) const;
112+
const TConfig& At(const TStringBuf& key) const;
113+
};
114+
}

0 commit comments

Comments
 (0)