Commit 3da7e2ad authored by Eric Myhre's avatar Eric Myhre

schema unification progress: map representations, enums, etc.

parent d74ecb3e
......@@ -84,10 +84,18 @@ func (c *Compiler) Init() {
}
}
func (c *Compiler) Compile() (TypeSystem, error) {
func (c *Compiler) Compile() (*TypeSystem, error) {
panic("TODO")
}
func (c *Compiler) MustCompile() *TypeSystem {
ts, err := c.Compile()
if err != nil {
panic(err)
}
return ts
}
func (c *Compiler) addType(t Type) {
c.mustHaveNameFree(t.Name())
c.ts.types[TypeReference(t.Name())] = t
......@@ -160,8 +168,12 @@ func (Compiler) MakeStructRepresentation_Map(fieldDetails structFieldNameStructR
//go:generate quickimmut -output=compiler_carriers.go -attach=Compiler map StructFieldName StructRepresentation_Map_FieldDetails
func (c *Compiler) TypeMap(name TypeName, keyTypeRef TypeName, valueTypeRef TypeReference, valueNullable bool) {
c.addType(&TypeMap{c.ts, name, keyTypeRef, valueTypeRef, valueNullable})
func (c *Compiler) TypeMap(name TypeName, keyTypeRef TypeName, valueTypeRef TypeReference, valueNullable bool, rstrat MapRepresentation) {
c.addType(&TypeMap{c.ts, name, keyTypeRef, valueTypeRef, valueNullable, rstrat})
}
func (Compiler) MakeMapRepresentation_Stringpairs(innerDelim string, entryDelim string) MapRepresentation {
return MapRepresentation_Stringpairs{innerDelim, entryDelim}
}
func (c *Compiler) TypeList(name TypeName, valueTypeRef TypeReference, valueNullable bool) {
......
......@@ -10,7 +10,7 @@ import (
// and so we've just chained it all together with switch statements;
// creating a separate interface per result type seems just not super relevant.
func (schdmt Schema) Compile() (schema.TypeSystem, error) {
func (schdmt Schema) Compile() (*schema.TypeSystem, error) {
c := &schema.Compiler{}
typesdmt := schdmt.FieldTypes()
for itr := typesdmt.Iterator(); !itr.Done(); {
......@@ -38,6 +38,7 @@ func (schdmt Schema) Compile() (schema.TypeSystem, error) {
schema.TypeName(t2.FieldKeyType().String()),
t2.FieldValueType().TypeReference(),
t2.FieldValueNullable().Bool(),
t2.FieldRepresentation().compile(c),
)
// If the field typeReference is TypeDefnInline, that needs a chance to take additional action.
t2.FieldValueType().compile(c)
......@@ -139,6 +140,19 @@ func (dmt TypeNameOrInlineDefn) compile(c *schema.Compiler) {
}
}
func (dmt MapRepresentation) compile(c *schema.Compiler) schema.MapRepresentation {
switch rdmt := dmt.AsInterface().(type) {
case MapRepresentation_Map:
return schema.MapRepresentation_Map{}
case MapRepresentation_Listpairs:
return schema.MapRepresentation_Listpairs{}
case MapRepresentation_Stringpairs:
return c.MakeMapRepresentation_Stringpairs(rdmt.FieldInnerDelim().String(), rdmt.FieldEntryDelim().String())
default:
panic("unreachable")
}
}
func (dmt StructRepresentation_Map) compile() schema.StructRepresentation {
if !dmt.FieldFields().Exists() {
return schema.Compiler{}.MakeStructRepresentation_Map(schema.Compiler{}.MakeStructFieldNameStructRepresentation_Map_FieldDetailsMap())
......
package schema
import (
"github.com/ipld/go-ipld-prime"
)
type TypeEnum struct {
name TypeName
members []string // a map in the dmt, but really an ordered set. easier as a slice in golang.
ts *TypeSystem
rstrat EnumRepresentation
}
func (t *TypeEnum) Representation() EnumRepresentation {
return t.rstrat
}
type EnumRepresentation interface{ _EnumRepresentation() }
func (EnumRepresentation_String) _EnumRepresentation() {}
func (EnumRepresentation_Int) _EnumRepresentation() {}
type EnumRepresentation_String struct {
labels map[string]string // member:label
}
type EnumRepresentation_Int struct {
labels map[string]int // member:label
}
// -- schema.Type interface satisfaction -->
var _ Type = (*TypeEnum)(nil)
func (t *TypeEnum) _Type() {}
func (t *TypeEnum) TypeSystem() *TypeSystem {
return t.ts
}
func (TypeEnum) TypeKind() TypeKind {
return TypeKind_Enum
}
func (t *TypeEnum) Name() TypeName {
return t.name
}
func (t TypeEnum) RepresentationBehavior() ipld.Kind {
switch t.rstrat.(type) {
case EnumRepresentation_String:
return ipld.Kind_String
case EnumRepresentation_Int:
return ipld.Kind_Int
default:
panic("unreachable")
}
}
......@@ -10,6 +10,24 @@ type TypeMap struct {
keyTypeRef TypeName // is a TypeName and not a TypeReference because it can't be an anon.
valueTypeRef TypeReference
valueNullable bool
rstrat MapRepresentation
}
func (t *TypeMap) Representation() MapRepresentation {
return t.rstrat
}
type MapRepresentation interface{ _MapRepresentation() }
func (MapRepresentation_Map) _MapRepresentation() {}
func (MapRepresentation_Listpairs) _MapRepresentation() {}
func (MapRepresentation_Stringpairs) _MapRepresentation() {}
type MapRepresentation_Map struct{}
type MapRepresentation_Listpairs struct{}
type MapRepresentation_Stringpairs struct {
innerDelim string
entryDelim string
}
// -- Type interface satisfaction -->
......
......@@ -22,6 +22,10 @@ type StructField struct {
type StructFieldName string
func (t *TypeStruct) Representation() StructRepresentation {
return t.rstrat
}
type StructRepresentation interface{ _StructRepresentation() }
func (StructRepresentation_Map) _StructRepresentation() {}
......
......@@ -11,6 +11,10 @@ type TypeUnion struct {
rstrat UnionRepresentation
}
func (t *TypeUnion) Representation() UnionRepresentation {
return t.rstrat
}
type UnionRepresentation interface{ _UnionRepresentation() }
func (UnionRepresentation_Keyed) _UnionRepresentation() {}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment