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

     1  package gengo
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/ipld/go-ipld-prime/schema"
     7  	"github.com/ipld/go-ipld-prime/schema/gen/go/mixins"
     8  )
     9  
    10  type stringGenerator struct {
    11  	AdjCfg *AdjunctCfg
    12  	mixins.StringTraits
    13  	PkgName string
    14  	Type    *schema.TypeString
    15  }
    16  
    17  func (stringGenerator) IsRepr() bool { return false } // hint used in some generalized templates.
    18  
    19  // --- native content and specializations --->
    20  
    21  func (g stringGenerator) EmitNativeType(w io.Writer) {
    22  	emitNativeType_scalar(w, g.AdjCfg, g)
    23  }
    24  func (g stringGenerator) EmitNativeAccessors(w io.Writer) {
    25  	emitNativeAccessors_scalar(w, g.AdjCfg, g)
    26  }
    27  func (g stringGenerator) EmitNativeBuilder(w io.Writer) {
    28  	// Generate a single-step construction function -- this is easy to do for a scalar,
    29  	//  and all representations of scalar kind can be expected to have a method like this.
    30  	// The function is attached to the NodePrototype for convenient namespacing;
    31  	//  it needs no new memory, so it would be inappropriate to attach to the builder or assembler.
    32  	// The function is directly used internally by anything else that might involve recursive destructuring on the same scalar kind
    33  	//  (for example, structs using stringjoin strategies that have one of this type as a field, etc).
    34  	// FUTURE: should engage validation flow.
    35  	doTemplate(`
    36  		func (_{{ .Type | TypeSymbol }}__Prototype) fromString(w *_{{ .Type | TypeSymbol }}, v string) error {
    37  			*w = _{{ .Type | TypeSymbol }}{v}
    38  			return nil
    39  		}
    40  	`, w, g.AdjCfg, g)
    41  	// And generate a publicly exported version of that single-step constructor, too.
    42  	//  (Just don't expose the details about allocation, because you can't meaningfully use that from outside the package.)
    43  	emitNativeBuilder_scalar(w, g.AdjCfg, g)
    44  }
    45  
    46  func (g stringGenerator) EmitNativeMaybe(w io.Writer) {
    47  	emitNativeMaybe(w, g.AdjCfg, g)
    48  }
    49  
    50  // --- type info --->
    51  
    52  func (g stringGenerator) EmitTypeConst(w io.Writer) {
    53  	doTemplate(`
    54  		// TODO EmitTypeConst
    55  	`, w, g.AdjCfg, g)
    56  }
    57  
    58  // --- TypedNode interface satisfaction --->
    59  
    60  func (g stringGenerator) EmitTypedNodeMethodType(w io.Writer) {
    61  	doTemplate(`
    62  		func ({{ .Type | TypeSymbol }}) Type() schema.Type {
    63  			return nil /*TODO:typelit*/
    64  		}
    65  	`, w, g.AdjCfg, g)
    66  }
    67  
    68  func (g stringGenerator) EmitTypedNodeMethodRepresentation(w io.Writer) {
    69  	emitTypicalTypedNodeMethodRepresentation(w, g.AdjCfg, g)
    70  }
    71  
    72  // --- Node interface satisfaction --->
    73  
    74  func (g stringGenerator) EmitNodeType(w io.Writer) {
    75  	// No additional types needed.  Methods all attach to the native type.
    76  }
    77  
    78  func (g stringGenerator) EmitNodeTypeAssertions(w io.Writer) {
    79  	emitNodeTypeAssertions_typical(w, g.AdjCfg, g)
    80  }
    81  func (g stringGenerator) EmitNodeMethodAsString(w io.Writer) {
    82  	emitNodeMethodAsKind_scalar(w, g.AdjCfg, g)
    83  }
    84  func (g stringGenerator) EmitNodeMethodPrototype(w io.Writer) {
    85  	emitNodeMethodPrototype_typical(w, g.AdjCfg, g)
    86  }
    87  func (g stringGenerator) EmitNodePrototypeType(w io.Writer) {
    88  	emitNodePrototypeType_typical(w, g.AdjCfg, g)
    89  }
    90  
    91  // --- NodeBuilder and NodeAssembler --->
    92  
    93  func (g stringGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
    94  	return stringBuilderGenerator{
    95  		g.AdjCfg,
    96  		mixins.StringAssemblerTraits{
    97  			PkgName:       g.PkgName,
    98  			TypeName:      g.TypeName,
    99  			AppliedPrefix: "_" + g.AdjCfg.TypeSymbol(g.Type) + "__",
   100  		},
   101  		g.PkgName,
   102  		g.Type,
   103  	}
   104  }
   105  
   106  type stringBuilderGenerator struct {
   107  	AdjCfg *AdjunctCfg
   108  	mixins.StringAssemblerTraits
   109  	PkgName string
   110  	Type    *schema.TypeString
   111  }
   112  
   113  func (stringBuilderGenerator) IsRepr() bool { return false } // hint used in some generalized templates.
   114  
   115  func (g stringBuilderGenerator) EmitNodeBuilderType(w io.Writer) {
   116  	emitEmitNodeBuilderType_typical(w, g.AdjCfg, g)
   117  }
   118  func (g stringBuilderGenerator) EmitNodeBuilderMethods(w io.Writer) {
   119  	emitNodeBuilderMethods_typical(w, g.AdjCfg, g)
   120  }
   121  func (g stringBuilderGenerator) EmitNodeAssemblerType(w io.Writer) {
   122  	emitNodeAssemblerType_scalar(w, g.AdjCfg, g)
   123  }
   124  func (g stringBuilderGenerator) EmitNodeAssemblerMethodAssignNull(w io.Writer) {
   125  	emitNodeAssemblerMethodAssignNull_scalar(w, g.AdjCfg, g)
   126  }
   127  func (g stringBuilderGenerator) EmitNodeAssemblerMethodAssignString(w io.Writer) {
   128  	emitNodeAssemblerMethodAssignKind_scalar(w, g.AdjCfg, g)
   129  }
   130  func (g stringBuilderGenerator) EmitNodeAssemblerMethodAssignNode(w io.Writer) {
   131  	emitNodeAssemblerMethodAssignNode_scalar(w, g.AdjCfg, g)
   132  }
   133  func (g stringBuilderGenerator) EmitNodeAssemblerOtherBits(w io.Writer) {
   134  	// Nothing needed here for string kinds.
   135  }