Commit dc442aca authored by Eric Myhre's avatar Eric Myhre

Add schema.Kind and Type.Kind(). Remove Type.ReprKind().

The schema.Type objects don't have a `Kind() ReprKind` method.  That
was just a bad idea.  I didn't try to fix it; I just nuked it.

The new schema.Type.Kind() method docs include a description of why
it's incorrect to wish for a either a single or a statically known
ReprKind for a type.

(Context mention: the previous commit was also a wind-up for this.
And technically, I'm still winding back up the mental stack here:
this had to get straightened out because I wanted to start poking
codegen for structs; and then I had beef with the TypeStruct.tupleStyle
field being a bool; and then fixing that pointed out that the current
Type.ReprKind methods were kinda insane... yaks!)
parent d912ea30
package schema
// Kind is an enum of kind in the IPLD Schema system.
//
// Note that schema.Kind is distinct from ipld.ReprKind!
// Schema kinds include concepts such as "struct" and "enum", which are
// concepts only introduced by the Schema layer, and not present in the
// Data Model layer.
type Kind uint8
const (
Kind_Invalid Kind = 0
Kind_Map Kind = '{'
Kind_List Kind = '['
Kind_Unit Kind = '1'
Kind_Bool Kind = 'b'
Kind_Int Kind = 'i'
Kind_Float Kind = 'f'
Kind_String Kind = 's'
Kind_Bytes Kind = 'x'
Kind_Link Kind = '/'
Kind_Union Kind = '^'
Kind_Struct Kind = '$'
Kind_Enum Kind = '%'
// FUTURE: Kind_Any = '?'?
)
func (k Kind) String() string {
switch k {
case Kind_Invalid:
return "Invalid"
case Kind_Map:
return "Map"
case Kind_List:
return "List"
case Kind_Unit:
return "Unit"
case Kind_Bool:
return "Bool"
case Kind_Int:
return "Int"
case Kind_Float:
return "Float"
case Kind_String:
return "String"
case Kind_Bytes:
return "Bytes"
case Kind_Link:
return "Link"
case Kind_Union:
return "Union"
case Kind_Struct:
return "Struct"
case Kind_Enum:
return "Enum"
default:
panic("invalid enumeration value!")
}
}
...@@ -52,14 +52,24 @@ type Type interface { ...@@ -52,14 +52,24 @@ type Type interface {
// that string will not be required to be unique. // that string will not be required to be unique.
Name() TypeName Name() TypeName
// Returns the Representation Kind in the IPLD Data Model that this type // Returns the Kind of this Type.
// is expected to be serialized as.
// //
// Note that in one case, this will return `ipld.ReprKind_Invalid` -- // The returned value is a 1:1 association with which of the concrete
// TypeUnion with Style=Kinded may be serialized as different kinds // "schema.Type*" structs this interface can be cast to.
// depending on their value, so we can't say from the type definition //
// alone what kind we expect. // Note that a schema.Kind is a different enum than ipld.ReprKind;
ReprKind() ipld.ReprKind // and furthermore, there's no strict relationship between them.
// typed.Node values can be described by *two* distinct ReprKinds:
// one which describes how the Node itself will act,
// and another which describes how the Node presents for serialization.
// For some combinations of Type and representation strategy, one or both
// of the ReprKinds can be determined statically; but not always:
// it can sometimes be necessary to inspect the value quite concretely
// (e.g., `typed.Node{}.Representation().ReprKind()`) in order to find
// out exactly how a node will be serialized! This is because some types
// can vary in representation kind based on their value (specifically,
// kinded-representation unions have this property).
Kind() Kind
} }
var ( var (
......
package schema package schema
import (
"github.com/ipld/go-ipld-prime"
)
/* cookie-cutter standard interface stuff */ /* cookie-cutter standard interface stuff */
func (anyType) _Type() {} func (anyType) _Type() {}
func (t anyType) TypeSystem() *TypeSystem { return t.universe } func (t anyType) TypeSystem() *TypeSystem { return t.universe }
func (t anyType) Name() TypeName { return t.name } func (t anyType) Name() TypeName { return t.name }
func (TypeBool) ReprKind() ipld.ReprKind { func (TypeBool) Kind() Kind { return Kind_Bool }
return ipld.ReprKind_Bool func (TypeString) Kind() Kind { return Kind_String }
} func (TypeBytes) Kind() Kind { return Kind_Bytes }
func (TypeString) ReprKind() ipld.ReprKind { func (TypeInt) Kind() Kind { return Kind_Int }
return ipld.ReprKind_String func (TypeFloat) Kind() Kind { return Kind_Float }
} func (TypeMap) Kind() Kind { return Kind_Map }
func (TypeBytes) ReprKind() ipld.ReprKind { func (TypeList) Kind() Kind { return Kind_List }
return ipld.ReprKind_Bytes func (TypeLink) Kind() Kind { return Kind_Link }
} func (TypeUnion) Kind() Kind { return Kind_Union }
func (TypeInt) ReprKind() ipld.ReprKind { func (TypeStruct) Kind() Kind { return Kind_Struct }
return ipld.ReprKind_Int func (TypeEnum) Kind() Kind { return Kind_Enum }
}
func (TypeFloat) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Float
}
func (TypeMap) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Map
}
func (TypeList) ReprKind() ipld.ReprKind {
return ipld.ReprKind_List
}
func (TypeLink) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Link
}
func (t TypeUnion) ReprKind() ipld.ReprKind {
if t.style == UnionStyle_Kinded {
return ipld.ReprKind_Invalid
} else {
return ipld.ReprKind_Map
}
}
func (t TypeStruct) ReprKind() ipld.ReprKind {
if t.tupleStyle {
return ipld.ReprKind_List
} else {
return ipld.ReprKind_Map
}
}
func (TypeEnum) ReprKind() ipld.ReprKind {
return ipld.ReprKind_String
}
/* interesting methods per Type type */ /* interesting methods per Type type */
......
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