genKindStringReprString.go 3.09 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1
package gengo
Eric Myhre's avatar
Eric Myhre committed
2 3 4 5 6 7

import (
	"io"
)

func (gk generateKindString) EmitNodeType(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
8
	doTemplate(`
9
		var _ ipld.Node = {{ .Type.Name }}{}
10
		var _ typed.Node = typed.Node(nil) // TODO
Eric Myhre's avatar
Eric Myhre committed
11

12
		type {{ .Type.Name }} struct{ x string }
13

Eric Myhre's avatar
Eric Myhre committed
14
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
15 16 17
}

func (gk generateKindString) EmitNodeMethodReprKind(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
18
	doTemplate(`
19
		func ({{ .Type.Name }}) ReprKind() ipld.ReprKind {
Eric Myhre's avatar
Eric Myhre committed
20 21
			return ipld.ReprKind_String
		}
Eric Myhre's avatar
Eric Myhre committed
22
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
23
}
24 25 26 27

// FUTURE: consider breaking the nodebuilder methods down like the node methods are; a lot of the "nope" variants could be reused.
func (gk generateKindString) EmitNodeMethodNodeBuilder(w io.Writer) {
	doTemplate(`
28 29
		func ({{ .Type.Name }}) NodeBuilder() ipld.NodeBuilder {
			return {{ .Type.Name }}__NodeBuilder{}
30 31
		}

32
		type {{ .Type.Name }}__NodeBuilder struct{}
33

34
		func (nb {{ .Type.Name }}__NodeBuilder) CreateMap() (ipld.MapBuilder, error) {
35 36
			return nil, ipld.ErrWrongKind{MethodName: "CreateMap", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}
		}
37
		func (nb {{ .Type.Name }}__NodeBuilder) AmendMap() (ipld.MapBuilder, error) {
38 39
			return nil, ipld.ErrWrongKind{MethodName: "AmendMap", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}
		}
40
		func (nb {{ .Type.Name }}__NodeBuilder) CreateList() (ipld.ListBuilder, error) {
41 42
			return nil, ipld.ErrWrongKind{MethodName: "CreateList", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}
		}
43
		func (nb {{ .Type.Name }}__NodeBuilder) AmendList() (ipld.ListBuilder, error) {
44 45
			return nil, ipld.ErrWrongKind{MethodName: "AmendList", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}
		}
46
		func (nb {{ .Type.Name }}__NodeBuilder) CreateNull() (ipld.Node, error) {
47 48
			return nil, ipld.ErrWrongKind{MethodName: "CreateNull", AppropriateKind: ipld.ReprKindSet_JustNull, ActualKind: ipld.ReprKind_String}
		}
49
		func (nb {{ .Type.Name }}__NodeBuilder) CreateBool(v bool) (ipld.Node, error) {
50 51
			return nil, ipld.ErrWrongKind{MethodName: "CreateBool", AppropriateKind: ipld.ReprKindSet_JustBool, ActualKind: ipld.ReprKind_String}
		}
52
		func (nb {{ .Type.Name }}__NodeBuilder) CreateInt(v int) (ipld.Node, error) {
53 54
			return nil, ipld.ErrWrongKind{MethodName: "CreateInt", AppropriateKind: ipld.ReprKindSet_JustInt, ActualKind: ipld.ReprKind_String}
		}
55
		func (nb {{ .Type.Name }}__NodeBuilder) CreateFloat(v float64) (ipld.Node, error) {
56 57
			return nil, ipld.ErrWrongKind{MethodName: "CreateFloat", AppropriateKind: ipld.ReprKindSet_JustFloat, ActualKind: ipld.ReprKind_String}
		}
58 59
		func (nb {{ .Type.Name }}__NodeBuilder) CreateString(v string) (ipld.Node, error) {
			return {{ .Type.Name }}{v}, nil
60
		}
61
		func (nb {{ .Type.Name }}__NodeBuilder) CreateBytes(v []byte) (ipld.Node, error) {
62 63
			return nil, ipld.ErrWrongKind{MethodName: "CreateBytes", AppropriateKind: ipld.ReprKindSet_JustBytes, ActualKind: ipld.ReprKind_String}
		}
64
		func (nb {{ .Type.Name }}__NodeBuilder) CreateLink(v ipld.Link) (ipld.Node, error) {
65 66 67 68
			return nil, ipld.ErrWrongKind{MethodName: "CreateLink", AppropriateKind: ipld.ReprKindSet_JustLink, ActualKind: ipld.ReprKind_String}
		}
	`, w, gk)
}