ctrlX Data Layer .NET API  4.3.0
Loading...
Searching...
No Matches
MetadataBuilder.cs
1using comm.datalayer;
2using Google.FlatBuffers;
3using System.Collections.Generic;
4
5namespace Datalayer
6{
10 public class MetadataBuilder
11 {
15 private readonly FlatBufferBuilder _builder;
16
20 private readonly List<Offset<Reference>> _references;
21
25 private readonly List<Offset<Extension>> _extensions;
26
30 private readonly List<Offset<LocaleText>> _displayNames;
31
35 private readonly List<Offset<LocaleText>> _descriptions;
36
40 private StringOffset _descriptionOffset;
41
45 private StringOffset _descriptionUrlOffset;
46
50 private NodeClass _nodeClass = NodeClass.Node;
51
55 private StringOffset _displayNameOffset;
56
60 private DisplayFormat _displayFormat = DisplayFormat.Auto;
61
65 private StringOffset _unitOffset;
66
70 private Offset<AllowedOperations> _allowedOperationsOffset;
71
78 public MetadataBuilder(AllowedOperationFlags allowedOperationFlags, string description = "",
79 string descriptionUrl = "")
80 {
81 _builder = new FlatBufferBuilder(Variant.DefaultFlatbuffersInitialSize);
82 _references = new List<Offset<Reference>>();
83 _extensions = new List<Offset<Extension>>();
84 _displayNames = new List<Offset<LocaleText>>();
85 _descriptions = new List<Offset<LocaleText>>();
86
87 // Set mandatory fields in constructor
88 _allowedOperationsOffset = AllowedOperations.CreateAllowedOperations(_builder,
89 (allowedOperationFlags & AllowedOperationFlags.Read) == AllowedOperationFlags.Read,
90 (allowedOperationFlags & AllowedOperationFlags.Write) == AllowedOperationFlags.Write,
91 (allowedOperationFlags & AllowedOperationFlags.Create) == AllowedOperationFlags.Create,
92 (allowedOperationFlags & AllowedOperationFlags.Delete) == AllowedOperationFlags.Delete,
93 (allowedOperationFlags & AllowedOperationFlags.Browse) == AllowedOperationFlags.Browse);
94 _descriptionOffset = _builder.CreateString(description);
95 _descriptionUrlOffset = _builder.CreateString(descriptionUrl);
96 }
97
103 public MetadataBuilder SetNodeClass(NodeClass nodeClass)
104 {
105 _nodeClass = nodeClass;
106 return this;
107 }
108
114 public MetadataBuilder SetDisplayName(string displayName)
115 {
116 _displayNameOffset = _builder.CreateString(displayName);
117 return this;
118 }
119
125 public MetadataBuilder SetDisplayFormat(DisplayFormat displayFormat)
126 {
127 _displayFormat = displayFormat;
128 return this;
129 }
130
136 public MetadataBuilder SetUnit(string unit)
137 {
138 _unitOffset = _builder.CreateString(unit);
139 return this;
140 }
141
148 public MetadataBuilder AddReference(ReferenceType type, string targetAddress)
149 {
150 _references.Add(Reference.CreateReference(_builder, _builder.CreateString(type.Value), _builder.CreateString(targetAddress)));
151 return this;
152 }
153
160 public MetadataBuilder AddExtension(string key, string value)
161 {
162 _extensions.Add(Extension.CreateExtension(_builder, _builder.CreateString(key), _builder.CreateString(value)));
163 return this;
164 }
165
172 public MetadataBuilder AddDisplayName(string localeId, string text)
173 {
174 _displayNames.Add(LocaleText.CreateLocaleText(_builder, _builder.CreateString(localeId), _builder.CreateString(text)));
175 return this;
176 }
177
184 public MetadataBuilder AddDescription(string localeId, string text)
185 {
186 _descriptions.Add(LocaleText.CreateLocaleText(_builder, _builder.CreateString(localeId), _builder.CreateString(text)));
187 return this;
188 }
189
194 public Variant Build()
195 {
196 var referencesVector = _references.Count == 0
197 ? new VectorOffset(0)
198 : Reference.CreateSortedVectorOfReference(_builder, _references.ToArray());
199 var extensionsVector = _extensions.Count == 0
200 ? new VectorOffset(0)
201 : Extension.CreateSortedVectorOfExtension(_builder, _extensions.ToArray());
202 var descriptionsVector = _descriptions.Count == 0
203 ? new VectorOffset(0)
204 : LocaleText.CreateSortedVectorOfLocaleText(_builder, _descriptions.ToArray());
205 var displayNamesVector = _displayNames.Count == 0
206 ? new VectorOffset(0)
207 : LocaleText.CreateSortedVectorOfLocaleText(_builder, _displayNames.ToArray());
208
209 var offsetMetadata = Metadata.CreateMetadata(_builder,
210 _nodeClass,
211 _allowedOperationsOffset,
212 _descriptionOffset,
213 _descriptionUrlOffset,
214 _displayNameOffset,
215 _displayFormat,
216 _unitOffset,
217 extensionsVector,
218 referencesVector,
219 descriptionsVector,
220 displayNamesVector);
221
222 Metadata.FinishMetadataBuffer(_builder, offsetMetadata);
223 return new Variant(_builder);
224 }
225 }
226}
Provides a convenient way to to build up a Metadata flatbuffers.
MetadataBuilder SetUnit(string unit)
Sets the unit.
MetadataBuilder AddReference(ReferenceType type, string targetAddress)
Adds the reference.
MetadataBuilder SetDisplayName(string displayName)
Sets the display name.
MetadataBuilder SetNodeClass(NodeClass nodeClass)
Sets the node class.
MetadataBuilder SetDisplayFormat(DisplayFormat displayFormat)
Sets the display format.
MetadataBuilder AddDescription(string localeId, string text)
Adds the description.
MetadataBuilder AddDisplayName(string localeId, string text)
Adds the display name.
Variant Build()
Builds this instance.
MetadataBuilder AddExtension(string key, string value)
Adds the extension.
MetadataBuilder(AllowedOperationFlags allowedOperationFlags, string description="", string descriptionUrl="")
Initializes a new instance of the MetadataBuilder class.
Represents a type of reference.
Definition: ReferenceType.cs:7
string Value
Gets the value.
Provides the implementation for IVariant.
Definition: Variant.cs:18
static readonly int DefaultFlatbuffersInitialSize
Gets the default Flatbuffers initial size in bytes.
Definition: Variant.cs:730
AllowedOperationFlags
The AllowedOperationFlags enumeration flags.
Definition: Enums.cs:10