Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten SATS + convenience APIs + more support #55

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/quickstart/client/module_bindings/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType()
{
new SpacetimeDB.SATS.ProductTypeElement("sender", SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
{
new SpacetimeDB.SATS.ProductTypeElement("__identity_bytes", SpacetimeDB.SATS.AlgebraicType.CreateArrayType(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U8))),
new SpacetimeDB.SATS.ProductTypeElement("__identity_bytes", SpacetimeDB.SATS.AlgebraicType.CreateBytesType()),
})),
new SpacetimeDB.SATS.ProductTypeElement("sent", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U64)),
new SpacetimeDB.SATS.ProductTypeElement("text", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String)),
new SpacetimeDB.SATS.ProductTypeElement("sent", SpacetimeDB.SATS.AlgebraicType.CreateU64Type()),
new SpacetimeDB.SATS.ProductTypeElement("text", SpacetimeDB.SATS.AlgebraicType.CreateStringType()),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void SendMessageDeserializeEventArgs(ClientApi.Event dbEvent)
bsatnBytes.CopyTo(ms.GetBuffer(), 0);
ms.Position = 0;
using var reader = new System.IO.BinaryReader(ms);
var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String), reader);
var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreateStringType(), reader);
args.Text = args_0_value.AsString();
dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.SendMessage, "send_message", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), dbEvent.Message, dbEvent.Status, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void SetNameDeserializeEventArgs(ClientApi.Event dbEvent)
bsatnBytes.CopyTo(ms.GetBuffer(), 0);
ms.Position = 0;
using var reader = new System.IO.BinaryReader(ms);
var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String), reader);
var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreateStringType(), reader);
args.Name = args_0_value.AsString();
dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.SetName, "set_name", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), dbEvent.Message, dbEvent.Status, args);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/quickstart/client/module_bindings/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType()
{
new SpacetimeDB.SATS.ProductTypeElement("identity", SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
{
new SpacetimeDB.SATS.ProductTypeElement("__identity_bytes", SpacetimeDB.SATS.AlgebraicType.CreateArrayType(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U8))),
new SpacetimeDB.SATS.ProductTypeElement("__identity_bytes", SpacetimeDB.SATS.AlgebraicType.CreateBytesType()),
})),
new SpacetimeDB.SATS.ProductTypeElement("name", SpacetimeDB.SATS.AlgebraicType.CreateSumType(new System.Collections.Generic.List<SpacetimeDB.SATS.SumTypeVariant>
{
new SpacetimeDB.SATS.SumTypeVariant("some", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String)),
new SpacetimeDB.SATS.SumTypeVariant("some", SpacetimeDB.SATS.AlgebraicType.CreateStringType()),
new SpacetimeDB.SATS.SumTypeVariant("none", SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
{
})),
})),
new SpacetimeDB.SATS.ProductTypeElement("online", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.Bool)),
new SpacetimeDB.SATS.ProductTypeElement("online", SpacetimeDB.SATS.AlgebraicType.CreateBoolType()),
});
}

Expand Down
20 changes: 1 addition & 19 deletions src/Identity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,7 @@ public struct Identity : IEquatable<Identity>

public byte[] Bytes => bytes;

public static AlgebraicType GetAlgebraicType()
{
return new AlgebraicType
{
type = AlgebraicType.Type.Builtin,
builtin = new BuiltinType
{
type = BuiltinType.Type.Array,
arrayType = new AlgebraicType
{
type = AlgebraicType.Type.Builtin,
builtin = new BuiltinType
{
type = BuiltinType.Type.U8
}
}
}
};
}
public static AlgebraicType GetAlgebraicType() => AlgebraicType.CreateBytesType();

public static explicit operator Identity(AlgebraicValue v) => new Identity
{
Expand Down
111 changes: 56 additions & 55 deletions src/SATS/AlgebraicType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@ public class SumType
{
public List<SumTypeVariant> variants;

public SumType()
{
variants = new List<SumTypeVariant>();
}
public SumType() => variants = new List<SumTypeVariant>();

// TODO(jdetter): Perhaps not needed?
public SumType NewUnnamed()
public SumType NewUnnamed() => new SumType
{
var s = new SumType
{
variants = variants.Select(a => new SumTypeVariant(null, a.algebraicType)).ToList()
};
return s;
}
variants = variants.Select(a => new SumTypeVariant(null, a.algebraicType)).ToList()
};
}

public struct SumTypeVariant
Expand All @@ -42,10 +35,7 @@ public class ProductType
{
public List<ProductTypeElement> elements;

public ProductType()
{
elements = new List<ProductTypeElement>();
}
public ProductType() => elements = new List<ProductTypeElement>();
}

public struct ProductTypeElement
Expand All @@ -72,10 +62,13 @@ public struct MapType
public AlgebraicType valueType;
}

public class BuiltinType
public class AlgebraicType
{
public enum Type
{
TypeRef,
Sum,
Product,
Bool,
I8,
U8,
Expand All @@ -91,23 +84,7 @@ public enum Type
F64,
String,
Array,
Map
}

public Type type;

public AlgebraicType arrayType;
public MapType mapType;
}

public class AlgebraicType
{
public enum Type
{
Sum,
Product,
Builtin,
TypeRef,
Map,
None,
}

Expand All @@ -121,20 +98,28 @@ public SumType sum {
type = value == null ? Type.None : Type.Sum;
}
}

public ProductType product {
get { return type == Type.Product ? (ProductType)type_ : null; }
set {
type_ = value;
type = value == null ? Type.None : Type.Product;
}
}
public BuiltinType builtin {
get { return type == Type.Builtin ? (BuiltinType)type_ : null; }

public AlgebraicType array {
get { return type == Type.Array ? (AlgebraicType)type_ : null; }
set {
type_ = value;
type = value == null ? Type.None : Type.Builtin;
type = value == null ? Type.None : Type.Array;
}
}

public MapType map {
get { return type == Type.Map ? (MapType)type_ : null; }
set {
type_ = value;
type = value == null ? Type.None : Type.Map;
}
}

Expand All @@ -151,19 +136,18 @@ public static AlgebraicType CreateProductType(IEnumerable<ProductTypeElement> el
return new AlgebraicType
{
type = Type.Product,
product = new ProductType
{
type_ = new ProductType {
elements = elements.ToList()
}
};
}

public static AlgebraicType CreateSumType(IEnumerable<SumTypeVariant> variants)
{
return new AlgebraicType
{
type = Type.Sum,
sum = new SumType
type_ = new SumType
{
variants = variants.ToList(),
}
Expand All @@ -173,25 +157,42 @@ public static AlgebraicType CreateSumType(IEnumerable<SumTypeVariant> variants)
public static AlgebraicType CreateArrayType(AlgebraicType elementType) {
return new AlgebraicType
{
type = Type.Builtin,
builtin = new BuiltinType
{
type = BuiltinType.Type.Array,
arrayType = elementType
}
type = Type.Array,
type_ = elementType
};
}

public static AlgebraicType CreatePrimitiveType(BuiltinType.Type type) {
public static AlgebraicType CreateBytesType() => AlgebraicType.CreateArrayType(AlgebraicType.CreateU8Type());

public static AlgebraicType CreateMapType(MapType type) {
return new AlgebraicType
{
type = Type.Builtin,
builtin = new BuiltinType
{
type = type,
}
type = Type.Map,
type_ = type
};
}

public static AlgebraicType CreateTypeRef(int idx) {
return new AlgebraicType
{
type = Type.TypeRef,
type_ = idx
};
}

public static AlgebraicType CreateBoolType() => new AlgebraicType { type = Type.Bool };
public static AlgebraicType CreateI8Type() => new AlgebraicType { type = Type.I8 };
public static AlgebraicType CreateU8Type() => new AlgebraicType { type = Type.U8 };
public static AlgebraicType CreateI16Type() => new AlgebraicType { type = Type.I16 };
public static AlgebraicType CreateU16Type() => new AlgebraicType { type = Type.U16 };
public static AlgebraicType CreateI32Type() => new AlgebraicType { type = Type.I32 };
public static AlgebraicType CreateU32Type() => new AlgebraicType { type = Type.U32 };
public static AlgebraicType CreateI64Type() => new AlgebraicType { type = Type.I64 };
public static AlgebraicType CreateU64Type() => new AlgebraicType { type = Type.U64 };
public static AlgebraicType CreateI128Type() => new AlgebraicType { type = Type.I128 };
public static AlgebraicType CreateU128Type() => new AlgebraicType { type = Type.U128 };
public static AlgebraicType CreateF32Type() => new AlgebraicType { type = Type.F32 };
public static AlgebraicType CreateF64Type() => new AlgebraicType { type = Type.F64 };
public static AlgebraicType CreateStringType() => new AlgebraicType { type = Type.String };
}
}
}
Loading