generate.go 2.51 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package gengo

import (
	"io"
	"os"
	"path/filepath"

	"github.com/ipld/go-ipld-prime/schema"
)

func Generate(pth string, pkgName string, ts schema.TypeSystem, adjCfg *AdjunctCfg) {
	// Emit fixed bits.
	withFile(filepath.Join(pth, "minima.go"), func(f io.Writer) {
		EmitInternalEnums(pkgName, f)
	})

	// Emit a file for each type.
	for _, typ := range ts.GetTypes() {
		withFile(filepath.Join(pth, "t"+typ.Name().String()+".go"), func(f io.Writer) {
			EmitFileHeader(pkgName, f)
			switch t2 := typ.(type) {
22
			case *schema.TypeBool:
Eric Myhre's avatar
Eric Myhre committed
23
				EmitEntireType(NewBoolReprBoolGenerator(pkgName, t2, adjCfg), f)
24
			case *schema.TypeInt:
Eric Myhre's avatar
Eric Myhre committed
25
				EmitEntireType(NewIntReprIntGenerator(pkgName, t2, adjCfg), f)
26
			case *schema.TypeFloat:
Eric Myhre's avatar
Eric Myhre committed
27
				EmitEntireType(NewFloatReprFloatGenerator(pkgName, t2, adjCfg), f)
28
			case *schema.TypeString:
Eric Myhre's avatar
Eric Myhre committed
29
				EmitEntireType(NewStringReprStringGenerator(pkgName, t2, adjCfg), f)
30
			case *schema.TypeBytes:
Eric Myhre's avatar
Eric Myhre committed
31
				EmitEntireType(NewBytesReprBytesGenerator(pkgName, t2, adjCfg), f)
32
			case *schema.TypeLink:
Eric Myhre's avatar
Eric Myhre committed
33
				EmitEntireType(NewLinkReprLinkGenerator(pkgName, t2, adjCfg), f)
34
			case *schema.TypeStruct:
Eric Myhre's avatar
Eric Myhre committed
35 36 37
				switch t2.RepresentationStrategy().(type) {
				case schema.StructRepresentation_Map:
					EmitEntireType(NewStructReprMapGenerator(pkgName, t2, adjCfg), f)
38 39
				case schema.StructRepresentation_Tuple:
					EmitEntireType(NewStructReprTupleGenerator(pkgName, t2, adjCfg), f)
40 41
				case schema.StructRepresentation_Stringjoin:
					EmitEntireType(NewStructReprStringjoinGenerator(pkgName, t2, adjCfg), f)
Eric Myhre's avatar
Eric Myhre committed
42 43 44
				default:
					panic("unrecognized struct representation strategy")
				}
45
			case *schema.TypeMap:
46
				EmitEntireType(NewMapReprMapGenerator(pkgName, t2, adjCfg), f)
47
			case *schema.TypeList:
Eric Myhre's avatar
Eric Myhre committed
48
				EmitEntireType(NewListReprListGenerator(pkgName, t2, adjCfg), f)
49
			case *schema.TypeUnion:
50 51 52
				switch t2.RepresentationStrategy().(type) {
				case schema.UnionRepresentation_Keyed:
					EmitEntireType(NewUnionReprKeyedGenerator(pkgName, t2, adjCfg), f)
53 54
				case schema.UnionRepresentation_Kinded:
					EmitEntireType(NewUnionReprKindedGenerator(pkgName, t2, adjCfg), f)
55 56 57
				default:
					panic("unrecognized union representation strategy")
				}
Eric Myhre's avatar
Eric Myhre committed
58 59 60 61 62
			default:
				panic("add more type switches here :)")
			}
		})
	}
63 64 65 66 67

	// Emit the unified type table.
	withFile(filepath.Join(pth, "typeTable.go"), func(f io.Writer) {
		EmitTypeTable(pkgName, ts, adjCfg, f)
	})
Eric Myhre's avatar
Eric Myhre committed
68 69 70 71 72 73 74 75 76 77
}

func withFile(filename string, fn func(io.Writer)) {
	f, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	fn(f)
}