github.com/ipld/go-ipld-prime@v0.21.0/adl/rot13adl/rot13substrate.go (about)

     1  package rot13adl
     2  
     3  import (
     4  	"github.com/ipld/go-ipld-prime/datamodel"
     5  	"github.com/ipld/go-ipld-prime/node/mixins"
     6  	"github.com/ipld/go-ipld-prime/schema"
     7  )
     8  
     9  // Substrate returns the root node of the raw internal data form of the ADL's content.
    10  func (n *_R13String) Substrate() datamodel.Node {
    11  	// This is a very minor twist in the case of the rot13 ADL.
    12  	//  However, for larger ADLs (especially those relating to multi-block collections),
    13  	//   this could be quite a bit more involved, and would almost certainly be the root node of a larger tree.
    14  	return (*_Substrate)(n)
    15  }
    16  
    17  // -- Node -->
    18  
    19  var _ datamodel.Node = (*_Substrate)(nil)
    20  
    21  // Somewhat unusually for an ADL, there's only one substrate node type,
    22  // and we actually made it have the same in-memory structure as the synthesized view node.
    23  //
    24  // When implementing other more complex ADLs, it will probably be more common to have
    25  // the synthesized high-level node type either embed or have a pointer to the substrate root node.
    26  type _Substrate _R13String
    27  
    28  // REVIEW: what on earth we think the "TypeName" strings in error messages and other references to this node should be.
    29  //  At the moment, it shares a prefix with the synthesized node, which is potentially confusing (?),
    30  //  and I'm not sure what, if any, suffix actually makes meaningful sense to a user either.
    31  //  I added the segment ".internal." to the middle of the name mangle; does this seem helpful?
    32  
    33  func (*_Substrate) Kind() datamodel.Kind {
    34  	return datamodel.Kind_String
    35  }
    36  func (*_Substrate) LookupByString(string) (datamodel.Node, error) {
    37  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupByString("")
    38  }
    39  func (*_Substrate) LookupByNode(datamodel.Node) (datamodel.Node, error) {
    40  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupByNode(nil)
    41  }
    42  func (*_Substrate) LookupByIndex(idx int64) (datamodel.Node, error) {
    43  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupByIndex(0)
    44  }
    45  func (*_Substrate) LookupBySegment(seg datamodel.PathSegment) (datamodel.Node, error) {
    46  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.LookupBySegment(seg)
    47  }
    48  func (*_Substrate) MapIterator() datamodel.MapIterator {
    49  	return nil
    50  }
    51  func (*_Substrate) ListIterator() datamodel.ListIterator {
    52  	return nil
    53  }
    54  func (*_Substrate) Length() int64 {
    55  	return -1
    56  }
    57  func (*_Substrate) IsAbsent() bool {
    58  	return false
    59  }
    60  func (*_Substrate) IsNull() bool {
    61  	return false
    62  }
    63  func (*_Substrate) AsBool() (bool, error) {
    64  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsBool()
    65  }
    66  func (*_Substrate) AsInt() (int64, error) {
    67  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsInt()
    68  }
    69  func (*_Substrate) AsFloat() (float64, error) {
    70  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsFloat()
    71  }
    72  func (n *_Substrate) AsString() (string, error) {
    73  	return n.raw, nil
    74  }
    75  func (*_Substrate) AsBytes() ([]byte, error) {
    76  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsBytes()
    77  }
    78  func (*_Substrate) AsLink() (datamodel.Link, error) {
    79  	return mixins.String{TypeName: "rot13adl.internal.Substrate"}.AsLink()
    80  }
    81  func (*_Substrate) Prototype() datamodel.NodePrototype {
    82  	return _Substrate__Prototype{}
    83  }
    84  
    85  // -- NodePrototype -->
    86  
    87  var _ datamodel.NodePrototype = _Substrate__Prototype{}
    88  
    89  type _Substrate__Prototype struct {
    90  	// There's no configuration to this ADL.
    91  }
    92  
    93  func (np _Substrate__Prototype) NewBuilder() datamodel.NodeBuilder {
    94  	return &_Substrate__Builder{}
    95  }
    96  
    97  // -- NodeBuilder -->
    98  
    99  var _ datamodel.NodeBuilder = (*_Substrate__Builder)(nil)
   100  
   101  type _Substrate__Builder struct {
   102  	_Substrate__Assembler
   103  }
   104  
   105  func (nb *_Substrate__Builder) Build() datamodel.Node {
   106  	if nb.m != schema.Maybe_Value {
   107  		panic("invalid state: cannot call Build on an assembler that's not finished")
   108  	}
   109  	return nb.w
   110  }
   111  func (nb *_Substrate__Builder) Reset() {
   112  	*nb = _Substrate__Builder{}
   113  }
   114  
   115  // -- NodeAssembler -->
   116  
   117  var _ datamodel.NodeAssembler = (*_Substrate__Assembler)(nil)
   118  
   119  type _Substrate__Assembler struct {
   120  	w *_Substrate
   121  	m schema.Maybe // REVIEW: if the package where this Maybe enum lives is maybe not the right home for it after all.  Or should this line use something different?  We're only using some of its values after all.
   122  }
   123  
   124  func (_Substrate__Assembler) BeginMap(sizeHint int64) (datamodel.MapAssembler, error) {
   125  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.BeginMap(0)
   126  }
   127  func (_Substrate__Assembler) BeginList(sizeHint int64) (datamodel.ListAssembler, error) {
   128  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.BeginList(0)
   129  }
   130  func (na *_Substrate__Assembler) AssignNull() error {
   131  	// REVIEW: unclear how this might compose with some other context (like a schema) which does allow nulls.  Probably a wrapper type?
   132  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignNull()
   133  }
   134  func (_Substrate__Assembler) AssignBool(bool) error {
   135  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignBool(false)
   136  }
   137  func (_Substrate__Assembler) AssignInt(int64) error {
   138  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignInt(0)
   139  }
   140  func (_Substrate__Assembler) AssignFloat(float64) error {
   141  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignFloat(0)
   142  }
   143  func (na *_Substrate__Assembler) AssignString(v string) error {
   144  	switch na.m {
   145  	case schema.Maybe_Value:
   146  		panic("invalid state: cannot assign into assembler that's already finished")
   147  	}
   148  	na.w = &_Substrate{
   149  		raw:         v,
   150  		synthesized: unrotate(v),
   151  	}
   152  	na.m = schema.Maybe_Value
   153  	return nil
   154  }
   155  func (_Substrate__Assembler) AssignBytes([]byte) error {
   156  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignBytes(nil)
   157  }
   158  func (_Substrate__Assembler) AssignLink(datamodel.Link) error {
   159  	return mixins.StringAssembler{TypeName: "rot13adl.internal.Substrate"}.AssignLink(nil)
   160  }
   161  func (na *_Substrate__Assembler) AssignNode(v datamodel.Node) error {
   162  	if v.IsNull() {
   163  		return na.AssignNull()
   164  	}
   165  	if v2, ok := v.(*_Substrate); ok {
   166  		switch na.m {
   167  		case schema.Maybe_Value:
   168  			panic("invalid state: cannot assign into assembler that's already finished")
   169  		}
   170  		na.w = v2
   171  		na.m = schema.Maybe_Value
   172  		return nil
   173  	}
   174  	if v2, err := v.AsString(); err != nil {
   175  		return err
   176  	} else {
   177  		return na.AssignString(v2)
   178  	}
   179  }
   180  func (_Substrate__Assembler) Prototype() datamodel.NodePrototype {
   181  	return _Substrate__Prototype{}
   182  }