type_enum.go 1.43 KB
Newer Older
1 2 3
package schema

import (
tavit ohanian's avatar
tavit ohanian committed
4 5
	"gitlab.dms3.io/ld/go-ld-prime"
	schemadmt "gitlab.dms3.io/ld/go-ld-prime/schema/dmt"
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
)

type TypeEnum struct {
	name TypeName
	dmt  schemadmt.TypeEnum
	ts   *TypeSystem
}

type EnumRepresentation interface{ _EnumRepresentation() }

func (EnumRepresentation_String) _EnumRepresentation() {}
func (EnumRepresentation_Int) _EnumRepresentation()    {}

type EnumRepresentation_String struct {
	dmt schemadmt.EnumRepresentation_String
}
type EnumRepresentation_Int struct {
	dmt schemadmt.EnumRepresentation_Int
}

// -- schema.Type interface satisfaction -->

var _ Type = (*TypeEnum)(nil)

func (t *TypeEnum) _Type() {}

func (t *TypeEnum) TypeSystem() *TypeSystem {
	return t.ts
}

36 37
func (TypeEnum) TypeKind() TypeKind {
	return TypeKind_Struct
38 39 40 41 42 43
}

func (t *TypeEnum) Name() TypeName {
	return t.name
}

tavit ohanian's avatar
tavit ohanian committed
44
func (t TypeEnum) RepresentationBehavior() ld.Kind {
45 46
	switch t.dmt.FieldRepresentation().AsInterface().(type) {
	case schemadmt.EnumRepresentation_String:
tavit ohanian's avatar
tavit ohanian committed
47
		return ld.Kind_String
48
	case schemadmt.EnumRepresentation_Int:
tavit ohanian's avatar
tavit ohanian committed
49
		return ld.Kind_Int
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	default:
		panic("unreachable")
	}
}

// -- specific to TypeEnum -->

func (t *TypeEnum) RepresentationStrategy() EnumRepresentation {
	switch x := t.dmt.FieldRepresentation().AsInterface().(type) {
	case schemadmt.EnumRepresentation_String:
		return EnumRepresentation_String{x}
	case schemadmt.EnumRepresentation_Int:
		return EnumRepresentation_Int{x}
	default:
		panic("unreachable")
	}
}