type_link.go 1.13 KB
Newer Older
1 2 3
package schema

import (
tavit ohanian's avatar
tavit ohanian committed
4
	ld "gitlab.dms3.io/ld/go-ld-prime"
tavit ohanian's avatar
tavit ohanian committed
5
	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
)

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

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

var _ Type = (*TypeLink)(nil)

func (t *TypeLink) _Type() {}

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

24 25
func (TypeLink) TypeKind() TypeKind {
	return TypeKind_Link
26 27 28 29 30 31
}

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

tavit ohanian's avatar
tavit ohanian committed
32 33
func (t TypeLink) RepresentationBehavior() ld.Kind {
	return ld.Kind_Link
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
}

// -- specific to TypeLink -->

// HasReferencedType returns true if the link has a hint about the type it references.
func (t *TypeLink) HasReferencedType() bool {
	return t.dmt.FieldExpectedType().Exists()
}

// ReferencedType returns the type which is expected for the node on the other side of the link.
// Nil is returned if there is no information about the expected type
// (which may be interpreted as "any").
func (t *TypeLink) ReferencedType() Type {
	if !t.dmt.FieldExpectedType().Exists() {
		return nil
	}
	return t.ts.types[t.dmt.FieldExpectedType().Must().TypeReference()]
}