using Google.Protobuf.WellKnownTypes;
namespace MFiles.Grpc.Samples;
///
/// Builders for the messages almost every call needs: object identities, typed values and
/// search conditions.
///
public static class MFilesValues
{
/// An object: its object type (0 = document) and ID.
public static ObjID ObjId(int objectType, int id) =>
new() { Type = objectType, ItemId = new ItemID { InternalId = id } };
/// A specific version of an object. Writes to a checked-in object need this.
public static ObjVer ObjVer(int objectType, int id, int version) => new()
{
ObjId = ObjId(objectType, id),
Version = new ObjVerVersion { Type = ObjVerVersionType.Specific, InternalVersion = version },
};
// [START typed-values]
/// A single-line text value.
public static TypedValue Text(string value) =>
new() { Type = Datatype.Text, Data = new TypedValueUnion { Text = value } };
/// An integer value.
public static TypedValue Integer(int value) =>
new() { Type = Datatype.Integer, Data = new TypedValueUnion { Integer = value } };
/// A Boolean value.
public static TypedValue Boolean(bool value) =>
new() { Type = Datatype.Boolean, Data = new TypedValueUnion { Boolean = value } };
/// A date value: midnight UTC of the day.
public static TypedValue Date(DateOnly date) => new()
{
Type = Datatype.Date,
Data = new TypedValueUnion
{
Date = date.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc).ToTimestamp(),
},
};
/// A timestamp value (date and time, UTC).
public static TypedValue Timestamp(DateTime utc) => new()
{
Type = Datatype.Timestamp,
Data = new TypedValueUnion { Timestamp = utc.ToTimestamp() },
};
/// A lookup: item of value list (or object type)
/// .
public static TypedValue Lookup(int valueList, int item) => new()
{
Type = Datatype.Lookup,
Data = new TypedValueUnion { Lookup = LookupItem(valueList, item) },
};
/// A multi-select lookup.
public static TypedValue MultiSelectLookup(int valueList, params int[] items) => new()
{
Type = Datatype.MultiSelectLookup,
Data = new TypedValueUnion
{
MultiSelectLookup = new MultiSelectLookup
{
Values = { items.Select(item => LookupItem(valueList, item)) },
},
},
};
/// An empty value of the given data type; it clears a property.
public static TypedValue Empty(Datatype type) => new() { Type = type, IsNullValue = true };
private static Lookup LookupItem(int valueList, int item) =>
new() { ValueListItemInfo = new ItemInfo { ObjId = ObjId(valueList, item) } };
// [END typed-values]
/// A property value: property definition .
public static PropertyValue Property(int propertyDef, TypedValue value) =>
new() { PropertyDef = propertyDef, Value = value };
// [START conditions]
/// A condition on a property value, e.g. property 100 (Class) equals a lookup.
public static SearchCondition PropertyCondition(
int propertyDef, ConditionType type, TypedValue value) => new()
{
Expression = new Expression
{
Type = ExpressionType.PropertyValue,
Data = new ExpressionUnion
{
PropertyValue = new ExpressionUnion.Types.PropertyValueData
{
PropertyDef = propertyDef,
},
},
},
Type = type,
Value = value,
};
/// A condition on a status value, e.g. the object type or the deleted flag.
public static SearchCondition StatusCondition(
StatusType status, ConditionType type, TypedValue value) => new()
{
Expression = new Expression
{
Type = ExpressionType.StatusValue,
Data = new ExpressionUnion
{
StatusValue = new ExpressionUnion.Types.StatusValueData { Type = status },
},
},
Type = type,
Value = value,
};
/// Objects of one object type. Value list -11 lists the object types.
public static SearchCondition IsObjectType(int objectType) =>
StatusCondition(StatusType.ObjectType, ConditionType.Equal, Lookup(-11, objectType));
/// Objects that are not deleted.
public static SearchCondition IsNotDeleted() =>
StatusCondition(StatusType.IsDeleted, ConditionType.Equal, Boolean(false));
// [END conditions]
}