Commit b6009032 authored by Eric Myhre's avatar Eric Myhre

schema: clear up some things about TypeName vs TypeReference.

Both are now accessible.  Name is not always present.

Get rid of casts that are unnecessary.

Constructors for anonymous types are still upcoming;
all the current constructors dupe the name into the reference field.
Planning to add distinct methods on the Compiler for anon types.
parent c6b9667d
......@@ -80,11 +80,12 @@ func (c *Compiler) MustCompile() *TypeSystem {
func (c *Compiler) addType(t Type) {
c.mustHaveNameFree(t.Name())
c.ts.types[TypeReference(t.Name())] = t
c.ts.types[t.Reference()] = t
c.ts.list = append(c.ts.list, t)
}
func (c *Compiler) addAnonType(t Type) {
c.ts.types[TypeReference(t.Name())] = t // FIXME it's... probably a bug that the Type.Name() method doesn't return a TypeReference. Yeah, it definitely is. TypeMap and TypeList should have their own name field internally be TypeReference, too, because it's true. wonder if we should have separate methods on the Type interface for this. would probably be a usability trap to do so, though (too many user printfs would use the Name function and get blanks and be surprised).
c.ts.types[t.Reference()] = t
c.ts.anonTypes = append(c.ts.anonTypes, t)
}
func (c *Compiler) mustHaveNameFree(name TypeName) {
......@@ -116,7 +117,7 @@ func (c *Compiler) TypeFloat(name TypeName) {
}
func (c *Compiler) TypeLink(name TypeName, expectedTypeRef TypeName) {
c.addType(&TypeLink{c.ts, name, expectedTypeRef})
c.addType(&TypeLink{c.ts, name, TypeReference(name), expectedTypeRef})
}
func (c *Compiler) TypeStruct(name TypeName, fields structFieldList, rstrat StructRepresentation) {
......@@ -151,7 +152,7 @@ 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, rstrat MapRepresentation) {
c.addType(&TypeMap{c.ts, name, keyTypeRef, valueTypeRef, valueNullable, rstrat})
c.addType(&TypeMap{c.ts, name, TypeReference(name), keyTypeRef, valueTypeRef, valueNullable, rstrat})
}
func (Compiler) MakeMapRepresentation_Stringpairs(innerDelim string, entryDelim string) MapRepresentation {
......@@ -159,7 +160,7 @@ func (Compiler) MakeMapRepresentation_Stringpairs(innerDelim string, entryDelim
}
func (c *Compiler) TypeList(name TypeName, valueTypeRef TypeReference, valueNullable bool) {
c.addType(&TypeList{c.ts, name, valueTypeRef, valueNullable})
c.addType(&TypeList{c.ts, name, TypeReference(name), valueTypeRef, valueNullable})
}
func (c *Compiler) TypeUnion(name TypeName, members typeNameList, rstrat UnionRepresentation) {
......
......@@ -50,7 +50,7 @@ func validate(ts *TypeSystem, typ Type, errs *[]error) {
newErrors := rule.rule(ts, typ)
if len(newErrors) > 0 {
for _, err := range newErrors {
*errs = append(*errs, ErrInvalidTypeSpec{rule.text, TypeReference(typ.Name()), err})
*errs = append(*errs, ErrInvalidTypeSpec{rule.text, typ.Reference(), err})
}
break
}
......@@ -92,7 +92,7 @@ var rules = map[TypeKind][]rule{
{"map declaration's value type must be defined",
alwaysApplies,
func(ts *TypeSystem, t Type) []error {
tRef := TypeReference(t.(*TypeMap).valueTypeRef)
tRef := t.(*TypeMap).valueTypeRef
if _, exists := ts.types[tRef]; !exists {
return []error{fmt.Errorf("missing type %q", tRef)}
}
......@@ -104,7 +104,7 @@ var rules = map[TypeKind][]rule{
{"list declaration's value type must be defined",
alwaysApplies,
func(ts *TypeSystem, t Type) []error {
tRef := TypeReference(t.(*TypeList).valueTypeRef)
tRef := t.(*TypeList).valueTypeRef
if _, exists := ts.types[tRef]; !exists {
return []error{fmt.Errorf("missing type %q", tRef)}
}
......@@ -117,6 +117,9 @@ var rules = map[TypeKind][]rule{
alwaysApplies,
func(ts *TypeSystem, t Type) []error {
tRef := TypeReference(t.(*TypeLink).expectedTypeRef)
if tRef == "" {
return nil
}
if _, exists := ts.types[tRef]; !exists {
return []error{fmt.Errorf("missing type %q", tRef)}
}
......@@ -129,7 +132,7 @@ var rules = map[TypeKind][]rule{
alwaysApplies,
func(ts *TypeSystem, t Type) (errs []error) {
for _, field := range t.(*TypeStruct).fields {
tRef := TypeReference(field.typeRef)
tRef := field.typeRef
if _, exists := ts.types[tRef]; !exists {
errs = append(errs, fmt.Errorf("missing type %q", tRef))
}
......
......@@ -57,12 +57,16 @@ type Type interface {
// Returns a pointer to the TypeSystem this Type is a member of.
TypeSystem() *TypeSystem
// Returns the string name of the Type. This name is unique within the
// universe this type is a member of, *unless* this type is Anonymous,
// in which case a string describing the type will still be returned, but
// that string will not be required to be unique.
// Returns the name of the Type, or, an empty string if this is an anonymous type.
// Use Reference instead if you always want a result, even for anonymous types.
Name() TypeName
// Reference returns a string reference for this Type, which is either the
// the type's name (if it has one) or a string that uniquely describes it
// if it's an anonymous type (this may be something like e.g. "[Foo]"
// or "{Foo:Bar}" or "&Foo").
Reference() TypeReference
// Returns the TypeKind of this Type.
//
// The returned value is a 1:1 association with which of the concrete
......
......@@ -25,6 +25,10 @@ func (t *TypeBool) Name() TypeName {
return t.name
}
func (t *TypeBool) Reference() TypeReference {
return TypeReference(t.name)
}
func (t TypeBool) RepresentationBehavior() ipld.Kind {
return ipld.Kind_Bool
}
......@@ -25,6 +25,10 @@ func (t *TypeBytes) Name() TypeName {
return t.name
}
func (t *TypeBytes) Reference() TypeReference {
return TypeReference(t.name)
}
func (t TypeBytes) RepresentationBehavior() ipld.Kind {
return ipld.Kind_Bytes
}
......@@ -45,6 +45,10 @@ func (t *TypeEnum) Name() TypeName {
return t.name
}
func (t *TypeEnum) Reference() TypeReference {
return TypeReference(t.name)
}
func (t TypeEnum) RepresentationBehavior() ipld.Kind {
switch t.rstrat.(type) {
case EnumRepresentation_String:
......
......@@ -25,6 +25,10 @@ func (t *TypeFloat) Name() TypeName {
return t.name
}
func (t *TypeFloat) Reference() TypeReference {
return TypeReference(t.name)
}
func (t TypeFloat) RepresentationBehavior() ipld.Kind {
return ipld.Kind_Float
}
......@@ -25,6 +25,10 @@ func (t *TypeInt) Name() TypeName {
return t.name
}
func (t *TypeInt) Reference() TypeReference {
return TypeReference(t.name)
}
func (t TypeInt) RepresentationBehavior() ipld.Kind {
return ipld.Kind_Int
}
......@@ -6,8 +6,9 @@ import (
type TypeLink struct {
ts *TypeSystem
name TypeName
expectedTypeRef TypeName // can be empty
name TypeName // may be empty if this is an anon type
ref TypeReference // may be dup of name, if this is a named type
expectedTypeRef TypeName // can be empty
}
// -- Type interface satisfaction -->
......@@ -26,6 +27,10 @@ func (t *TypeLink) Name() TypeName {
return t.name
}
func (t *TypeLink) Reference() TypeReference {
return t.ref
}
func (t TypeLink) RepresentationBehavior() ipld.Kind {
return ipld.Kind_Link
}
......
......@@ -6,7 +6,8 @@ import (
type TypeList struct {
ts *TypeSystem
name TypeName
name TypeName // may be empty if this is an anon type
ref TypeReference // may be dup of name, if this is a named type
valueTypeRef TypeReference
valueNullable bool
}
......@@ -27,6 +28,10 @@ func (t *TypeList) Name() TypeName {
return t.name
}
func (t *TypeList) Reference() TypeReference {
return t.ref
}
func (t TypeList) RepresentationBehavior() ipld.Kind {
return ipld.Kind_List
}
......
......@@ -6,8 +6,9 @@ import (
type TypeMap struct {
ts *TypeSystem
name TypeName
keyTypeRef TypeName // is a TypeName and not a TypeReference because it can't be an anon.
name TypeName // may be empty if this is an anon type
ref TypeReference // may be dup of name, if this is a named type
keyTypeRef TypeName // is a TypeName and not a TypeReference because it can't be an anon.
valueTypeRef TypeReference
valueNullable bool
rstrat MapRepresentation
......@@ -46,6 +47,10 @@ func (t *TypeMap) Name() TypeName {
return t.name
}
func (t *TypeMap) Reference() TypeReference {
return t.ref
}
func (t TypeMap) RepresentationBehavior() ipld.Kind {
return ipld.Kind_Map
}
......@@ -64,7 +69,7 @@ func (t *TypeMap) KeyType() Type {
// ValueType returns the Type of the map values.
func (t *TypeMap) ValueType() Type {
return t.ts.types[TypeReference(t.valueTypeRef)]
return t.ts.types[t.valueTypeRef]
}
// ValueIsNullable returns a bool describing if the map values are permitted to be null.
......
......@@ -25,6 +25,10 @@ func (t *TypeString) Name() TypeName {
return t.name
}
func (t *TypeString) Reference() TypeReference {
return TypeReference(t.name)
}
func (t TypeString) RepresentationBehavior() ipld.Kind {
return ipld.Kind_String
}
......@@ -76,6 +76,10 @@ func (t *TypeStruct) Name() TypeName {
return t.name
}
func (t *TypeStruct) Reference() TypeReference {
return TypeReference(t.name)
}
func (t TypeStruct) RepresentationBehavior() ipld.Kind {
switch t.rstrat.(type) {
case StructRepresentation_Map:
......
......@@ -68,6 +68,10 @@ func (t *TypeUnion) Name() TypeName {
return t.name
}
func (t *TypeUnion) Reference() TypeReference {
return TypeReference(t.name)
}
func (t *TypeUnion) RepresentationBehavior() ipld.Kind {
switch t.rstrat.(type) {
case UnionRepresentation_Keyed:
......
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