-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathDmlToolsConfig.cs
More file actions
207 lines (181 loc) · 7.39 KB
/
Copy pathDmlToolsConfig.cs
File metadata and controls
207 lines (181 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
namespace Azure.DataApiBuilder.Config.ObjectModel;
/// <summary>
/// DML Tools configuration that can be either a boolean or object with individual tool settings
/// </summary>
public record DmlToolsConfig
{
/// <summary>
/// Default value for all tools when not specified
/// </summary>
public const bool DEFAULT_ENABLED = true;
/// <summary>
/// Indicates if all tools are enabled/disabled uniformly
/// </summary>
public bool AllToolsEnabled { get; init; }
/// <summary>
/// Whether describe-entities tool is enabled
/// </summary>
public bool? DescribeEntities { get; init; }
/// <summary>
/// Whether create-record tool is enabled
/// </summary>
public bool? CreateRecord { get; init; }
/// <summary>
/// Whether read-records tool is enabled
/// </summary>
public bool? ReadRecords { get; init; }
/// <summary>
/// Whether update-record tool is enabled
/// </summary>
public bool? UpdateRecord { get; init; }
/// <summary>
/// Whether delete-record tool is enabled
/// </summary>
public bool? DeleteRecord { get; init; }
/// <summary>
/// Whether execute-entity tool is enabled
/// </summary>
public bool? ExecuteEntity { get; init; }
/// <summary>
/// Whether aggregate-records tool is enabled
/// </summary>
public bool? AggregateRecords { get; init; }
[JsonConstructor]
public DmlToolsConfig(
bool? allToolsEnabled = null,
bool? describeEntities = null,
bool? createRecord = null,
bool? readRecords = null,
bool? updateRecord = null,
bool? deleteRecord = null,
bool? executeEntity = null,
bool? aggregateRecords = null)
{
if (allToolsEnabled is not null)
{
AllToolsEnabled = allToolsEnabled.Value;
UserProvidedAllTools = true;
// When allToolsEnabled is set, use it as the default for all tools
bool toolDefault = allToolsEnabled.Value;
DescribeEntities = describeEntities ?? toolDefault;
CreateRecord = createRecord ?? toolDefault;
ReadRecords = readRecords ?? toolDefault;
UpdateRecord = updateRecord ?? toolDefault;
DeleteRecord = deleteRecord ?? toolDefault;
ExecuteEntity = executeEntity ?? toolDefault;
AggregateRecords = aggregateRecords ?? toolDefault;
}
else
{
AllToolsEnabled = DEFAULT_ENABLED;
// Set values with defaults
DescribeEntities = describeEntities ?? DEFAULT_ENABLED;
CreateRecord = createRecord ?? DEFAULT_ENABLED;
ReadRecords = readRecords ?? DEFAULT_ENABLED;
UpdateRecord = updateRecord ?? DEFAULT_ENABLED;
DeleteRecord = deleteRecord ?? DEFAULT_ENABLED;
ExecuteEntity = executeEntity ?? DEFAULT_ENABLED;
AggregateRecords = aggregateRecords ?? DEFAULT_ENABLED;
}
// Track user-provided status - only true if the parameter was not null
UserProvidedDescribeEntities = describeEntities is not null;
UserProvidedCreateRecord = createRecord is not null;
UserProvidedReadRecords = readRecords is not null;
UserProvidedUpdateRecord = updateRecord is not null;
UserProvidedDeleteRecord = deleteRecord is not null;
UserProvidedExecuteEntity = executeEntity is not null;
UserProvidedAggregateRecords = aggregateRecords is not null;
}
/// <summary>
/// Creates a DmlToolsConfig with all tools set to the same state
/// Used when user explicitly sets "dml-tools": true/false
/// </summary>
public static DmlToolsConfig FromBoolean(bool enabled)
{
// Only pass allToolsEnabled, leave individual tools as null
return new DmlToolsConfig(
allToolsEnabled: enabled,
describeEntities: null,
createRecord: null,
readRecords: null,
updateRecord: null,
deleteRecord: null,
executeEntity: null,
aggregateRecords: null
);
}
/// <summary>
/// Creates a default DmlToolsConfig with all tools enabled
/// Used when dml-tools is not specified in config at all
/// </summary>
public static DmlToolsConfig Default => new(
allToolsEnabled: null,
describeEntities: null,
createRecord: null,
readRecords: null,
updateRecord: null,
deleteRecord: null,
executeEntity: null,
aggregateRecords: null
);
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write all-tools-enabled
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(AllToolsEnabled))]
public bool UserProvidedAllTools { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write describe-entities
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(DescribeEntities))]
public bool UserProvidedDescribeEntities { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write create-record
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(CreateRecord))]
public bool UserProvidedCreateRecord { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write read-records
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(ReadRecords))]
public bool UserProvidedReadRecords { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write update-record
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(UpdateRecord))]
public bool UserProvidedUpdateRecord { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write delete-record
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(DeleteRecord))]
public bool UserProvidedDeleteRecord { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write execute-entity
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(ExecuteEntity))]
public bool UserProvidedExecuteEntity { get; init; } = false;
/// <summary>
/// Flag which informs CLI and JSON serializer whether to write aggregate-records
/// property/value to the runtime config file.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
[MemberNotNullWhen(true, nameof(AggregateRecords))]
public bool UserProvidedAggregateRecords { get; init; } = false;
}