@@ -4,7 +4,7 @@ using System.Collections.Generic;
44#nullable disable
55#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
66#pragma warning disable RS0016 // Add public types and members to the declared API
7- #pragma warning disable 618 // Member is obsolete
7+ #pragma warning disable CS0618 // Member is obsolete
88
99namespace Npgsql
1010{
@@ -13,18 +13,18 @@ namespace Npgsql
1313 private partial int Init()
1414 {
1515 // Set the strongly-typed properties to their default values
16- {{
16+ {{~
1717 for p in properties
1818 if p.is_obsolete
1919 continue
2020 end
2121
2222 if (p.default_value != null)
23- }}
23+ ~ }}
2424 {{ p.name }} = {{ p.default_value }};
25- {{
25+ {{~
2626 end
27- end }}
27+ end ~ }}
2828
2929 // Setting the strongly-typed properties here also set the string-based properties in the base class.
3030 // Clear them (default settings = empty connection string)
@@ -33,91 +33,59 @@ namespace Npgsql
3333 return 0;
3434 }
3535
36- private partial int GeneratedSetter( string keyword, object value)
36+ private partial bool GeneratedActions(GeneratedAction action, string keyword, ref object value)
3737 {
3838 switch (keyword)
3939 {
40- {{ for kv in properties_by_keyword }}
40+ {{~ for kv in properties_by_keyword ~ }}
4141 case "{{ kv.key }}":
42- {{ p = kv.value }}
43- {{ if p.is_enum }}
42+ {{~ for alternative in kv.value.alternatives ~}}
43+ case "{{ alternative }}":
44+ {{~ end ~}}
4445 {
45- {{ p.name }} = value is string s
46- ? ({{ p.type_name }})Enum.Parse(typeof({{ p.type_name }}), s, ignoreCase: true)
47- : ({{ p.type_name }})Convert.ChangeType(value, typeof({{ p.type_name }}));
48- }
49- {{ else }}
50- {{ p.name }} = ({{ p.type_name }})Convert.ChangeType(value, typeof({{ p.type_name }}));
51- {{ end }}
52- break;
53- {{ end }}
54-
55- default:
56- throw new KeyNotFoundException();
57- }
58-
59- return 0;
60- }
61-
62- private partial bool TryGetValueGenerated(string keyword, out object value)
63- {
64- switch (keyword)
65- {
66- {{ for kv in properties_by_keyword }}
67- case "{{ kv.key }}":
68- {{ p = kv.value }}
69- value = (object){{ p.name }} ?? "";
46+ {{~ p = kv.value ~}}
47+ const string canonicalName = "{{ p.canonical_name }}";
48+ switch(action)
49+ {
50+ case GeneratedAction.Remove:
51+ var removed = base.ContainsKey(canonicalName);
52+ {{~ if p.default_value == null ~}}
53+ {{ p.name }} = default;
54+ {{~ else ~}}
55+ {{ p.name }} = {{ p.default_value }};
56+ {{~ end ~}}
57+ {{~ if p.type_name != "String" ~}}
58+ base.Remove(canonicalName);
59+ {{~ else ~}}
60+ // String property setters call SetValue, which itself calls base.Remove().
61+ {{~ end ~}}
62+ return removed;
63+ case GeneratedAction.Set:
64+ {{~ if p.is_enum ~}}
65+ {{ p.name }} = ({{ p.type_name }})GetValue(typeof({{ p.type_name }}), value);
66+ {{~ else ~}}
67+ {{ p.name }} = ({{ p.type_name }})Convert.ChangeType(value, typeof({{ p.type_name }}));
68+ {{~ end ~}}
69+ break;
70+ case GeneratedAction.Get:
71+ value = (object){{ p.name }} ?? "";
72+ break;
73+ case GeneratedAction.GetCanonical:
74+ value = canonicalName;
75+ break;
76+ }
7077 return true;
71- {{ end }}
7278 }
73-
74- value = null;
75- return false;
76- }
77-
78- private partial bool ContainsKeyGenerated(string keyword)
79- => keyword switch
80- {
81- {{ for kv in properties_by_keyword }}
82- "{{ kv.key }}" => true,
83- {{ end }}
84-
85- _ => false
86- };
87-
88- private partial bool RemoveGenerated(string keyword)
89- {
90- switch (keyword)
91- {
92- {{ for kv in properties_by_keyword }}
93- case "{{ kv.key }}":
94- {
95- {{ p = kv.value }}
96- var removed = base.ContainsKey("{{ p.canonical_name }}");
97- // Note that string property setters call SetValue, which itself calls base.Remove().
98- {{ if p.default_value == null }}
99- {{ p.name }} = default;
100- {{ else }}
101- {{ p.name }} = {{ p.default_value }};
102- {{ end }}
103- base.Remove("{{ p.canonical_name }}");
104- return removed;
105- }
106- {{ end }}
107-
108- default:
109- throw new KeyNotFoundException();
79+ {{~ end ~}}
11080 }
81+ if (action is GeneratedAction.Get or GeneratedAction.GetCanonical)
82+ return false;
83+ throw new KeyNotFoundException();
84+
85+ static object GetValue(Type type, object value)
86+ => value is string s
87+ ? Enum.Parse(type, s, ignoreCase: true)
88+ : Convert.ChangeType(value, type);
11189 }
112-
113- private partial string ToCanonicalKeyword(string keyword)
114- => keyword switch
115- {
116- {{ for kv in properties_by_keyword }}
117- "{{ kv.key }}" => "{{ kv.value.canonical_name }}",
118- {{ end }}
119-
120- _ => throw new KeyNotFoundException()
121- };
12290 }
12391}
0 commit comments