typeMethods.go 4.44 KB
Newer Older
1
package typesystem
2

3 4 5 6
import (
	"github.com/ipld/go-ipld-prime"
)

7 8 9 10 11 12
/* cookie-cutter standard interface stuff */

func (anyType) _Type()                {}
func (t anyType) Universe() *Universe { return t.universe }
func (t anyType) Name() TypeName      { return t.name }

13 14
func (TypeBool) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Bool
15
}
16 17
func (TypeString) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_String
18
}
19 20
func (TypeBytes) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Bytes
21
}
22 23
func (TypeInt) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Int
24
}
25 26
func (TypeFloat) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Float
27
}
28 29
func (TypeMap) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Map
30
}
31 32
func (TypeList) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_List
33
}
34 35
func (TypeLink) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Link
36
}
37 38
func (t TypeUnion) ReprKind() ipld.ReprKind {
	if t.style == UnionStyle_Kinded {
39
		return ipld.ReprKind_Invalid
40
	} else {
41
		return ipld.ReprKind_Map
42 43
	}
}
44 45
func (t TypeObject) ReprKind() ipld.ReprKind {
	if t.tupleStyle {
46
		return ipld.ReprKind_List
47
	} else {
48
		return ipld.ReprKind_Map
49 50
	}
}
51 52
func (TypeEnum) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_String
53
}
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

/* interesting methods per Type type */

// IsAnonymous is returns true if the type was unnamed.  Unnamed types will
// claim to have a Name property like `{Foo:Bar}`, and this is not guaranteed
// to be a unique string for all types in the universe.
func (t TypeMap) IsAnonymous() bool {
	return t.anonymous
}

// KeyType returns the Type of the map keys.
//
// Note that map keys will must always be some type which is representable as a
// string in the IPLD Data Model (e.g. either TypeString or TypeEnum).
func (t TypeMap) KeyType() Type {
	return t.keyType
}

// ValueType returns to the Type of the map values.
func (t TypeMap) ValueType() Type {
	return t.valueType
}

// ValueIsNullable returns a bool describing if the map values are permitted
// to be null.
func (t TypeMap) ValueIsNullable() bool {
	return t.valueNullable
}

// IsAnonymous is returns true if the type was unnamed.  Unnamed types will
// claim to have a Name property like `[Foo]`, and this is not guaranteed
// to be a unique string for all types in the universe.
func (t TypeList) IsAnonymous() bool {
	return t.anonymous
}

// ValueType returns to the Type of the list values.
func (t TypeList) ValueType() Type {
	return t.valueType
}

// ValueIsNullable returns a bool describing if the list values are permitted
// to be null.
func (t TypeList) ValueIsNullable() bool {
	return t.valueNullable
}

// UnionMembers returns a set of all the types that can inhabit this Union.
func (t TypeUnion) UnionMembers() map[TypeName]Type {
	m := make(map[TypeName]Type, len(t.values))
	for k, v := range t.values {
		m[k] = v
	}
	return m
}

// Fields returns a slice of descriptions of the object's fields.
func (t TypeObject) Fields() []ObjectField {
	a := make([]ObjectField, len(t.fields))
	for i := range t.fields {
		a[i] = t.fields[i]
	}
	return a
}

// Name returns the string name of this field.  The name is the string that
// will be used as a map key if the structure this field is a member of is
// serialized as a map representation.
func (f ObjectField) Name() string { return f.name }

// Type returns the Type of this field's value.  Note the field may
// also be unset if it is either Optional or Nullable.
func (f ObjectField) Type() Type { return f.typ }

// IsOptional returns true if the field is allowed to be absent from the object.
// If IsOptional is false, the field may be absent from the serial representation
// of the object entirely.
//
// Note being optional is different than saying the value is permitted to be null!
// A field may be both nullable and optional simultaneously, or either, or neither.
func (f ObjectField) IsOptional() bool { return f.optional }

// IsNullable returns true if the field value is allowed to be null.
//
// If is Nullable is false, note that it's still possible that the field value
// will be absent if the field is Optional!  Being nullable is unrelated to
// whether the field's presence is optional as a whole.
//
// Note that a field may be both nullable and optional simultaneously,
// or either, or neither.
func (f ObjectField) IsNullable() bool { return f.nullable }

// Members returns a slice the strings which are valid inhabitants of this enum.
func (t TypeEnum) Members() []string {
	a := make([]string, len(t.members))
	for i := range t.members {
		a[i] = t.members[i]
	}
	return a
}