tmpBuilders.go 3.97 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3 4 5 6 7 8 9 10 11
package schema

// Everything in this file is __a temporary hack__ and will be __removed__.
//
// These methods will only hang around until more of the "ast" packages are finished;
// thereafter, building schema.Type and schema.TypeSystem values will only be
// possible through first constructing a schema AST, and *then* using Reify(),
// which will validate things correctly, cycle-check, cross-link, etc.
//
// (Meanwhile, we're using these methods in the codegen prototypes.)

12 13 14 15 16 17 18 19 20 21 22 23 24
// These methods use Type objects as parameters when pointing to other things,
//  but this is... turning out consistently problematic.
//   Even when we're doing this hacky direct-call doesn't-need-to-be-serializable temp stuff,
//    as written, this doesn't actually let us express cyclic things viably!
//   The same initialization questions are also going to come up again when we try to make
//    concrete values in the output of codegen.
// Maybe it's actually just a bad idea to have our reified Type types use Type pointers at all.
//  (I will never get tired of the tongue twisters, evidently.)
//  I'm not actually using that much, and it's always avoidable (it's trivial to replace with a map lookup bouncing through a 'ts' variable somewhere).
//  And having the AST gen'd types be... just... the thing... sounds nice.  It could save a lot of work.
//   (It would mean the golang types don't tell you whether the values have been checked for global properties or not, but, eh.)
//   (It's not really compatible with "Prototype and Type are the same thing for codegen'd stuff", either (or, we need more interfaces, and to *really* lean into them), but maybe that's okay.)

Eric Myhre's avatar
Eric Myhre committed
25
func SpawnString(name TypeName) TypeString {
26
	return TypeString{typeBase{name, nil}}
Eric Myhre's avatar
Eric Myhre committed
27 28
}

29
func SpawnInt(name TypeName) TypeInt {
30
	return TypeInt{typeBase{name, nil}}
31 32 33
}

func SpawnBytes(name TypeName) TypeBytes {
34
	return TypeBytes{typeBase{name, nil}}
35 36 37
}

func SpawnLink(name TypeName) TypeLink {
38
	return TypeLink{typeBase{name, nil}, nil, false}
39 40
}

41
func SpawnLinkReference(name TypeName, referenceType Type) TypeLink {
42
	return TypeLink{typeBase{name, nil}, referenceType, true}
43
}
44

hannahhoward's avatar
hannahhoward committed
45
func SpawnList(name TypeName, typ Type, nullable bool) TypeList {
46
	return TypeList{typeBase{name, nil}, false, typ, nullable}
hannahhoward's avatar
hannahhoward committed
47 48
}

49
func SpawnMap(name TypeName, keyType Type, valueType Type, nullable bool) TypeMap {
50
	return TypeMap{typeBase{name, nil}, false, keyType, valueType, nullable}
51 52
}

Eric Myhre's avatar
Eric Myhre committed
53
func SpawnStruct(name TypeName, fields []StructField, repr StructRepresentation) TypeStruct {
54
	v := TypeStruct{
55
		typeBase{name, nil},
56 57 58
		fields,
		make(map[string]StructField, len(fields)),
		repr,
59
	}
60 61 62 63
	for i := range fields {
		fields[i].parent = &v
		v.fieldsMap[fields[i].name] = fields[i]
	}
64 65 66 67 68 69 70 71
	switch repr.(type) {
	case StructRepresentation_Stringjoin:
		for _, f := range fields {
			if f.IsMaybe() {
				panic("neither nullable nor optional is supported on struct stringjoin representation")
			}
		}
	}
72
	return v
Eric Myhre's avatar
Eric Myhre committed
73 74
}
func SpawnStructField(name string, typ Type, optional bool, nullable bool) StructField {
75
	return StructField{nil /*populated later*/, name, typ, optional, nullable}
Eric Myhre's avatar
Eric Myhre committed
76
}
77 78 79
func SpawnStructRepresentationMap(renames map[string]string) StructRepresentation_Map {
	return StructRepresentation_Map{renames, nil}
}
80 81 82
func SpawnStructRepresentationStringjoin(delim string) StructRepresentation_Stringjoin {
	return StructRepresentation_Stringjoin{delim}
}
83

84 85 86 87 88 89 90
func SpawnUnion(name TypeName, members []Type, repr UnionRepresentation) TypeUnion {
	return TypeUnion{typeBase{name, nil}, members, repr}
}
func SpawnUnionRepresentationKeyed(table map[string]Type) UnionRepresentation_Keyed {
	return UnionRepresentation_Keyed{table}
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104
// The methods relating to TypeSystem are also mutation-heavy and placeholdery.

func (ts *TypeSystem) Init() {
	ts.namedTypes = make(map[TypeName]Type)
}
func (ts *TypeSystem) Accumulate(typ Type) {
	ts.namedTypes[typ.Name()] = typ
}
func (ts TypeSystem) GetTypes() map[TypeName]Type {
	return ts.namedTypes
}
func (ts TypeSystem) TypeByName(n string) Type {
	return ts.namedTypes[TypeName(n)]
}