github.com/ipld/go-ipld-prime@v0.21.0/schema/gen/go/genpartsMinima.go (about) 1 package gengo 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/ipld/go-ipld-prime/testutil" 8 ) 9 10 // EmitInternalEnums creates a file with enum types used internally. 11 // For example, the state machine values used in map and list builders. 12 // These always need to exist exactly once in each package created by codegen. 13 // 14 // The file header and import statements are included in the output of this function. 15 // (The imports in this file are different than most others in codegen output; 16 // we gather up any references to other packages in this file in order to simplify the rest of codegen's awareness of imports.) 17 func EmitInternalEnums(packageName string, w io.Writer) { 18 fmt.Fprint(w, testutil.Dedent(` 19 package `+packageName+` 20 21 `+doNotEditComment+` 22 23 import ( 24 "fmt" 25 26 "github.com/ipld/go-ipld-prime/datamodel" 27 "github.com/ipld/go-ipld-prime/schema" 28 ) 29 30 `)) 31 32 // The 'Maybe' enum does double-duty in this package as a state machine for assembler completion. 33 // 34 // The 'Maybe_Absent' value gains the additional semantic of "clear to assign (but not null)" 35 // (which works because if you're *in* a value assembler, "absent" as a final result is already off the table). 36 // Additionally, we get a few extra states that we cram into the same area of bits: 37 // - `midvalue` is used by assemblers of recursives to block AssignNull after BeginX. 38 // - `allowNull` is used by parent assemblers when initializing a child assembler to tell the child a transition to Maybe_Null is allowed in this context. 39 fmt.Fprint(w, testutil.Dedent(` 40 const ( 41 midvalue = schema.Maybe(4) 42 allowNull = schema.Maybe(5) 43 ) 44 45 `)) 46 47 fmt.Fprint(w, testutil.Dedent(` 48 type maState uint8 49 50 const ( 51 maState_initial maState = iota 52 maState_midKey 53 maState_expectValue 54 maState_midValue 55 maState_finished 56 ) 57 58 type laState uint8 59 60 const ( 61 laState_initial laState = iota 62 laState_midValue 63 laState_finished 64 ) 65 `)) 66 67 // We occasionally need this erroring thunk to be able to snake an error out from some assembly processes. 68 // It implements all of datamodel.NodeAssembler, but all of its methods return errors when used. 69 fmt.Fprint(w, testutil.Dedent(` 70 type _ErrorThunkAssembler struct { 71 e error 72 } 73 74 func (ea _ErrorThunkAssembler) BeginMap(_ int64) (datamodel.MapAssembler, error) { return nil, ea.e } 75 func (ea _ErrorThunkAssembler) BeginList(_ int64) (datamodel.ListAssembler, error) { return nil, ea.e } 76 func (ea _ErrorThunkAssembler) AssignNull() error { return ea.e } 77 func (ea _ErrorThunkAssembler) AssignBool(bool) error { return ea.e } 78 func (ea _ErrorThunkAssembler) AssignInt(int64) error { return ea.e } 79 func (ea _ErrorThunkAssembler) AssignFloat(float64) error { return ea.e } 80 func (ea _ErrorThunkAssembler) AssignString(string) error { return ea.e } 81 func (ea _ErrorThunkAssembler) AssignBytes([]byte) error { return ea.e } 82 func (ea _ErrorThunkAssembler) AssignLink(datamodel.Link) error { return ea.e } 83 func (ea _ErrorThunkAssembler) AssignNode(datamodel.Node) error { return ea.e } 84 func (ea _ErrorThunkAssembler) Prototype() datamodel.NodePrototype { 85 panic(fmt.Errorf("cannot get prototype from error-carrying assembler: already derailed with error: %w", ea.e)) 86 } 87 `)) 88 }