github.com/ipld/go-ipld-prime@v0.21.0/schema/gen/go/genpartsStrictoid.go (about)

     1  package gengo
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // What's "strictoid" mean?  Anything that's recursive but not infinite -- so structs, unions.
     8  
     9  // This form of BeginMap method is reusable for anything that has fixed size and thus ignores sizehint.
    10  // That means: structs, unions, and their relevant representations.
    11  func emitNodeAssemblerMethodBeginMap_strictoid(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
    12  	// We currently disregard sizeHint.  It's not relevant to us.
    13  	//  We could check it strictly and emit errors; presently, we don't.
    14  	// This method contains a branch to support MaybeUsesPtr because new memory may need to be allocated.
    15  	//  This allocation only happens if the 'w' ptr is nil, which means we're being used on a Maybe;
    16  	//  otherwise, the 'w' ptr should already be set, and we fill that memory location without allocating, as usual.
    17  	//  (Note that the Maybe we're talking about here is for us, not our children: so it applies on unions too.)
    18  	doTemplate(`
    19  		func (na *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) BeginMap(int64) (datamodel.MapAssembler, error) {
    20  			switch *na.m {
    21  			case schema.Maybe_Value, schema.Maybe_Null:
    22  				panic("invalid state: cannot assign into assembler that's already finished")
    23  			case midvalue:
    24  				panic("invalid state: it makes no sense to 'begin' twice on the same assembler!")
    25  			}
    26  			*na.m = midvalue
    27  			{{- if .Type | MaybeUsesPtr }}
    28  			if na.w == nil {
    29  				na.w = &_{{ .Type | TypeSymbol }}{}
    30  			}
    31  			{{- end}}
    32  			return na, nil
    33  		}
    34  	`, w, adjCfg, data)
    35  }