@@ -12,7 +12,10 @@ class TSecretId {
1212private:
1313 YDB_READONLY_PROTECT_DEF (TString, OwnerUserId);
1414 YDB_READONLY_PROTECT_DEF (TString, SecretId);
15+
1516public:
17+ inline static const TString PrefixWithUser = " USId:" ;
18+
1619 TSecretId () = default ;
1720 TSecretId (const TString& ownerUserId, const TString& secretId)
1821 : OwnerUserId(ownerUserId)
@@ -31,7 +34,7 @@ class TSecretId {
3134 if (proto.HasValue ()) {
3235 return proto.GetValue ();
3336 } else {
34- return TStringBuilder () << " USId: " << (proto.GetSecretOwnerId () ? proto.GetSecretOwnerId () : defaultOwnerId) << " :" << SecretId;
37+ return TStringBuilder () << PrefixWithUser << (proto.GetSecretOwnerId () ? proto.GetSecretOwnerId () : defaultOwnerId) << " :" << SecretId;
3538 }
3639 }
3740
@@ -43,18 +46,41 @@ class TSecretId {
4346 }
4447};
4548
49+ class TSecretName {
50+ private:
51+ YDB_READONLY_DEF (TString, SecretId);
52+
53+ public:
54+ inline static const TString PrefixNoUser = " SId:" ;
55+
56+ TSecretName () = default ;
57+ TSecretName (const TString& secretId) : SecretId(secretId) {}
58+
59+ TString SerializeToString () const {
60+ return TStringBuilder () << " SId:" << SecretId;
61+ }
62+
63+ bool DeserializeFromString (const TString& secretString) {
64+ if (secretString.StartsWith (PrefixNoUser)) {
65+ SecretId = secretString.substr (PrefixNoUser.size ());
66+ return true ;
67+ }
68+ return false ;
69+ }
70+ };
71+
4672class TSecretIdOrValue {
4773private:
48- YDB_READONLY_DEF (std::optional<TSecretId>, SecretId);
49- YDB_READONLY_DEF (std::optional<TString>, Value);
74+ using TState = std::variant<std::monostate, TSecretId, TSecretName, TString>;
75+ YDB_READONLY_DEF (TState, State);
76+
77+ private:
5078 TSecretIdOrValue () = default ;
5179
52- bool DeserializeFromStringImpl (const TString& info, const TString& defaultUserId) {
53- static const TString prefixWithUser = " USId:" ;
54- static const TString prefixNoUser = " SId:" ;
55- if (info.StartsWith (prefixWithUser)) {
80+ bool DeserializeFromStringImpl (const TString& info, const TString& defaultUserId = " " ) {
81+ if (info.StartsWith (TSecretId::PrefixWithUser)) {
5682 TStringBuf sb (info.data (), info.size ());
57- sb.Skip (prefixWithUser .size ());
83+ sb.Skip (TSecretId::PrefixWithUser .size ());
5884 TStringBuf uId;
5985 TStringBuf sId ;
6086 if (!sb.TrySplit (' :' , uId, sId )) {
@@ -63,32 +89,37 @@ class TSecretIdOrValue {
6389 if (!uId || !sId ) {
6490 return false ;
6591 }
66- SecretId = TSecretId (uId, sId );
67- } else if (info.StartsWith (prefixNoUser )) {
92+ State = TSecretId (uId, sId );
93+ } else if (info.StartsWith (TSecretName::PrefixNoUser )) {
6894 TStringBuf sb (info.data (), info.size ());
69- sb.Skip (prefixNoUser.size ());
70- SecretId = TSecretId (defaultUserId, TString (sb));
71- if (!sb || !defaultUserId) {
95+ sb.Skip (TSecretName::PrefixNoUser.size ());
96+ if (!sb) {
7297 return false ;
7398 }
99+ if (defaultUserId) {
100+ State = TSecretId (defaultUserId, TString (sb));
101+ } else {
102+ State = TSecretName (TString (sb));
103+ }
74104 } else {
75- Value = info;
105+ State = info;
76106 }
77107 return true ;
78108 }
79- explicit TSecretIdOrValue (const TSecretId& id)
80- : SecretId(id) {
81109
110+ explicit TSecretIdOrValue (const TSecretId& id)
111+ : State(id) {
112+ }
113+ explicit TSecretIdOrValue (const TSecretName& id)
114+ : State(id) {
82115 }
83-
84116 explicit TSecretIdOrValue (const TString& value)
85- : Value(value) {
86-
117+ : State(value) {
87118 }
88119
89120public:
90121 bool operator !() const {
91- return !Value && !SecretId ;
122+ return std::holds_alternative<std::monostate>(State) ;
92123 }
93124
94125 static TSecretIdOrValue BuildAsValue (const TString& value) {
@@ -103,12 +134,18 @@ class TSecretIdOrValue {
103134 return TSecretIdOrValue (id);
104135 }
105136
106- static std::optional<TSecretIdOrValue> DeserializeFromOptional (const NKikimrSchemeOp::TSecretableVariable& proto, const TString& secretInfo, const TString& defaultOwnerId = Default<TString>()) {
137+ static TSecretIdOrValue BuildAsId (const TSecretName& id) {
138+ return TSecretIdOrValue (id);
139+ }
140+
141+ static std::optional<TSecretIdOrValue> DeserializeFromOptional (
142+ const NKikimrSchemeOp::TSecretableVariable& proto, const TString& secretInfo, const TString& defaultOwnerId = Default<TString>()) {
107143 if (proto.HasSecretId ()) {
108144 return DeserializeFromProto (proto, defaultOwnerId);
109145 } else if (proto.HasValue ()) {
110146 return DeserializeFromString (proto.GetValue ().GetData ());
111- } if (secretInfo) {
147+ }
148+ if (secretInfo) {
112149 return DeserializeFromString (secretInfo, defaultOwnerId);
113150 } else {
114151 return {};
@@ -117,16 +154,25 @@ class TSecretIdOrValue {
117154
118155 NKikimrSchemeOp::TSecretableVariable SerializeToProto () const {
119156 NKikimrSchemeOp::TSecretableVariable result;
120- if (SecretId) {
121- result.MutableSecretId ()->SetId (SecretId->GetSecretId ());
122- result.MutableSecretId ()->SetOwnerId (SecretId->GetOwnerUserId ());
123- } else if (Value) {
124- result.MutableValue ()->SetData (*Value);
125- }
157+ std::visit (TOverloaded (
158+ [](std::monostate){ },
159+ [&result](const TSecretId& id){
160+ result.MutableSecretId ()->SetId (id.GetSecretId ());
161+ result.MutableSecretId ()->SetOwnerId (id.GetOwnerUserId ());
162+ },
163+ [&result](const TSecretName& name){
164+ result.MutableSecretId ()->SetId (name.GetSecretId ());
165+ },
166+ [&result](const TString& value){
167+ result.MutableValue ()->SetData (value);
168+ }
169+ ),
170+ State);
126171 return result;
127172 }
128173
129- static std::optional<TSecretIdOrValue> DeserializeFromProto (const NKikimrSchemeOp::TSecretableVariable& proto, const TString& defaultOwnerId = Default<TString>()) {
174+ static std::optional<TSecretIdOrValue> DeserializeFromProto (
175+ const NKikimrSchemeOp::TSecretableVariable& proto, const TString& defaultOwnerId = Default<TString>()) {
130176 if (proto.HasSecretId ()) {
131177 TString ownerId;
132178 TString secretId;
@@ -157,12 +203,21 @@ class TSecretIdOrValue {
157203 }
158204
159205 TString SerializeToString () const {
160- if (SecretId) {
161- return SecretId->SerializeToString ();
162- } else if (Value) {
163- return *Value;
164- }
165- return " " ;
206+ return std::visit (TOverloaded (
207+ [](std::monostate) -> TString{
208+ return " " ;
209+ },
210+ [](const TSecretId& id) -> TString{
211+ return TStringBuilder () << TSecretId::PrefixWithUser << id.GetOwnerUserId () << " :" << id.GetSecretId ();
212+ },
213+ [](const TSecretName& name) -> TString{
214+ return TStringBuilder () << TSecretName::PrefixNoUser << name.GetSecretId ();
215+ },
216+ [](const TString& value) -> TString{
217+ return value;
218+ }
219+ ),
220+ State);
166221 }
167222
168223 TString DebugString () const ;
0 commit comments