public static RecordStructuralTypeMember FromJson(Json.Expression expression) { if (expression is not Json.ArrayExpression array) { throw new BonsaiParseException("Expected a JSON array for a record structural type member definition.", expression); } if (array.ElementCount != 2) { throw new BonsaiParseException("Expected 2 JSON array elements for a record structural type member definition.", expression); } var nameExpr = array.GetElement(0); if (nameExpr.NodeType != Json.ExpressionType.String) { throw new BonsaiParseException("Expected a JSON string in 'node[0]' for the name of a record structural type member definition.", expression); } var name = (string)((Json.ConstantExpression)nameExpr).Value; var type = TypeRef.FromJson(array.GetElement(1)); return(new RecordStructuralTypeMember { Name = name, Type = type }); }
public static GenericTypeDef FromJson(Json.ArrayExpression type) { if (type.ElementCount != 3) { throw new BonsaiParseException("Expected 3 JSON array elements for a generic type definition.", type); } var typeDef = TypeRef.FromJson(type.GetElement(1)); if (type.GetElement(2) is not Json.ArrayExpression args) { throw new BonsaiParseException("Expected a JSON array in 'node[2]' for the type arguments of a generic type definition.", type); } var n = args.ElementCount; if (n < 1) { throw new BonsaiParseException("Expected at least 1 JSON array element in 'node[2]' for the type arguments of a generic type definition.", type); } var typeArgs = new TypeRef[n]; for (var i = 0; i < n; i++) { var arg = args.GetElement(i); var typeRef = TypeRef.FromJson(arg); typeArgs[i] = typeRef; } return(new GenericTypeDef(typeDef, typeArgs)); }
public static AnonymousStructuralTypeMember FromJson(Json.Expression expression) { if (expression is not Json.ArrayExpression array) { throw new BonsaiParseException("Expected a JSON array for an anonymous structural type member definition.", expression); } var count = array.ElementCount; if (count is not 2 and not 3) { throw new BonsaiParseException("Expected 2 or 3 JSON array elements for an anonymous structural type member definition.", expression); } var nameExpr = array.GetElement(0); if (nameExpr.NodeType != Json.ExpressionType.String) { throw new BonsaiParseException("Expected a JSON string in 'node[0]' for the name of an anonymous structural type member definition.", expression); } var name = (string)((Json.ConstantExpression)nameExpr).Value; var type = TypeRef.FromJson(array.GetElement(1)); var isKey = true; if (count == 3) { var isKeyExpr = array.GetElement(2); if (isKeyExpr.NodeType != Json.ExpressionType.Boolean) { throw new BonsaiParseException("Expected a JSON Boolean in 'node[2]' for IsKey flag of an anonymous structural type member definition.", expression); } isKey = (bool)((Json.ConstantExpression)isKeyExpr).Value; } return(new AnonymousStructuralTypeMember { Name = name, Type = type, IsKey = isKey }); }
public static ArrayTypeDef FromJson(Json.ArrayExpression type) { var count = type.ElementCount; if (count is not(2 or 3)) { throw new BonsaiParseException("Expected 2 or 3 JSON array elements for an array type definition.", type); } var elementType = TypeRef.FromJson(type.GetElement(1)); var rank = default(int?); if (count == 3) { if (type.GetElement(2) is not Json.ConstantExpression rankJson || !int.TryParse((string)rankJson.Value, out int value)) { throw new BonsaiParseException("Expected a JSON number in 'node[2]' for the rank of an array type definition.", type); } rank = value; } return(new ArrayTypeDef(elementType, rank)); }