IOneOfType
IOneOfType is interface for one-of type.
/// <summary>Description of one-of type</summary>
/// <remarks>Valid one-of has <see cref="IFieldContainerType.Fields"/> assigned.</remarks>
public interface IOneOfType : IDataType, IFieldContainerType, ICyclical
{
}
/// <summary>Interface for datatypes that hold <see cref="IFieldType"/>.</summary>
public interface IFieldContainerType : IDataType
{
/// <summary>List of fields.</summary>
//[RequiredMember]
IFieldType[] Fields { get; set; }
}
IDataType └── IOneOfType
OneOfType is the default implementation.
IOneOfType oneOfType = new OneOfType
{
Name = Identities.CreateName("Message").SetReadOnly(),
Annotations = { },
Description = "One of",
Unassignable = true,
Referable = true,
Fields = new IFieldType[]
{
new FieldType
{
Value = new RecordType("Result")
.AddField(new FieldType("Value", DataTypes.String.Instance))
.SetReadOnly()
}.SetOrder(1),
new FieldType
{
Value = new RecordType("Error")
.AddField(new FieldType("ErrorCode", DataTypes.Int32.Instance))
.AddField(new FieldType("Message", DataTypes.String.Instance))
.SetReadOnly()
}.SetOrder(2)
}
}
.SetReadOnly();
Message : IOneOfType ├── Fields[0] = IFieldType │ └── Value = Result : IRecordType │ └── Fields[0] = Value : IFieldType │ └── Value = string : IStringType │ └── Element = byte : IIntegerType └── Fields[1] = IFieldType └── Value = Error : IRecordType ├── Fields[0] = ErrorCode : IFieldType │ └── Value = int32 : IIntegerType └── Fields[1] = Message : IFieldType └── Value = string : IStringType
OneOfTypeRequest is request for an oneOf type when sub-requests are needed.
// Create service
IService service = Services.Create(NetTypeHandlers.Instance);
// Create request
OneOfTypeRequest request = new OneOfTypeRequest
{
Name = Identities.CreateName("Message"),
Annotations = new Dictionary<string, string> { { IDataType.Annotation.Description, "One of" } },
Unassignable = null,
Referable = null,
Fields = new object[] {
new FieldTypeRequest
{
Value = new RecordTypeRequest
{
Name = Identities.CreateName("Result"),
Annotations = new Dictionary<string, string> { { IFieldType.Annotation.Order, "1" } },
Fields = new object[] {
new FieldTypeRequest
{
Name = Identities.CreateName("Value"),
Value = DataTypes.String.Instance
},
}
},
},
new FieldTypeRequest
{
Value = new RecordTypeRequest
{
Name = Identities.CreateName("Error"),
Annotations = new Dictionary<string, string> { { IFieldType.Annotation.Order, "2" } },
Fields = new object[] {
new FieldTypeRequest
{
Name = Identities.CreateName("ErrorCode"),
Value = DataTypes.Int32.Instance
},
new FieldTypeRequest
{
Name = Identities.CreateName("Message"),
Value = DataTypes.String.Instance
},
}
}
}
}
};
// Query
IOneOfType datatype = service.GetRequired<OneOfTypeRequest, IOneOfType>(request);
Message : IOneOfType ├── Fields[0] = IFieldType └── Fields[1] = IFieldType └── Value = Result : IRecordType └── Fields[0] = IFieldType
Full Example
Full example
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using Avalanche.DataType;
using Avalanche.Identity;
using Avalanche.Service;
using Avalanche.Utilities;
using static System.Console;
public class oneoftype
{
public static void Run()
{
{
// <01>
IOneOfType oneOfType = new OneOfType
{
Name = Identities.CreateName("Message").SetReadOnly(),
Annotations = { },
Description = "One of",
Unassignable = true,
Referable = true,
Fields = new IFieldType[]
{
new FieldType
{
Value = new RecordType("Result")
.AddField(new FieldType("Value", DataTypes.String.Instance))
.SetReadOnly()
}.SetOrder(1),
new FieldType
{
Value = new RecordType("Error")
.AddField(new FieldType("ErrorCode", DataTypes.Int32.Instance))
.AddField(new FieldType("Message", DataTypes.String.Instance))
.SetReadOnly()
}.SetOrder(2)
}
}
.SetReadOnly();
// </01>
// Print
WriteLine(oneOfType.PrintTree());
}
{
// <03>
// Create service
IService service = Services.Create(NetTypeHandlers.Instance);
// Create request
OneOfTypeRequest request = new OneOfTypeRequest
{
Name = Identities.CreateName("Message"),
Annotations = new Dictionary<string, string> { { IDataType.Annotation.Description, "One of" } },
Unassignable = null,
Referable = null,
Fields = new object[] {
new FieldTypeRequest
{
Value = new RecordTypeRequest
{
Name = Identities.CreateName("Result"),
Annotations = new Dictionary<string, string> { { IFieldType.Annotation.Order, "1" } },
Fields = new object[] {
new FieldTypeRequest
{
Name = Identities.CreateName("Value"),
Value = DataTypes.String.Instance
},
}
},
},
new FieldTypeRequest
{
Value = new RecordTypeRequest
{
Name = Identities.CreateName("Error"),
Annotations = new Dictionary<string, string> { { IFieldType.Annotation.Order, "2" } },
Fields = new object[] {
new FieldTypeRequest
{
Name = Identities.CreateName("ErrorCode"),
Value = DataTypes.Int32.Instance
},
new FieldTypeRequest
{
Name = Identities.CreateName("Message"),
Value = DataTypes.String.Instance
},
}
}
}
}
};
// Query
IOneOfType datatype = service.GetRequired<OneOfTypeRequest, IOneOfType>(request);
// </03>
// Print
WriteLine(datatype.PrintTree());
}
}
}