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

     1  /*
     2  rot13adl is a demo ADL -- its purpose is to show what an ADL and its public interface can look like.
     3  It implements a "rot13" string: when creating data through the ADL, the user gives it a regular string;
     4  the ADL will create aninternal representation of it which has the characters altered in a reversable way.
     5  
     6  It provides reference and example materal, but it's very unlikely you want to use it in real situations ;)
     7  
     8  There are several ways to move data in and out of the ADL:
     9  
    10    - treat it like a regular IPLD map:
    11    - using the exported NodePrototype can be used to get a NodeBuilder which can accept keys and values;
    12    - using the resulting Node and doing lookup operations on it like a regular map;
    13    - load up raw substrate data and `Reify()` it into the synthesized form, and *then* treat it like a regular map:
    14    - this is handy if the raw data already parsed into Nodes.
    15    - optionally, use `SubstrateRootPrototype` as the prototype for loading the raw substrate data;
    16      any kind of Node is a valid input to Reify, but this one will generally have optimal performance.
    17    - take the synthesized form and inspect its substrate data:
    18    - the `Substrate()` method will return another datamodel.Node which is the root of the raw substrate data,
    19      and can be walked normally like any other datamodel.Node.
    20  */
    21  package rot13adl
    22  
    23  import (
    24  	"github.com/ipld/go-ipld-prime/datamodel"
    25  	"github.com/ipld/go-ipld-prime/node/mixins"
    26  	"github.com/ipld/go-ipld-prime/schema"
    27  )
    28  
    29  // -- Node -->
    30  
    31  var _ datamodel.Node = (R13String)(nil)
    32  
    33  type R13String = *_R13String
    34  
    35  type _R13String struct {
    36  	raw         string // the raw content, before our ADL lens is applied to it.
    37  	synthesized string // the content that the ADL presents.  calculated proactively from the original, in this implementation (though you could imagine implementing it lazily, in either direction, too).
    38  }
    39  
    40  func (*_R13String) Kind() datamodel.Kind {
    41  	return datamodel.Kind_String
    42  }
    43  func (*_R13String) LookupByString(string) (datamodel.Node, error) {
    44  	return mixins.String{TypeName: "rot13adl.R13String"}.LookupByString("")
    45  }
    46  func (*_R13String) LookupByNode(datamodel.Node) (datamodel.Node, error) {
    47  	return mixins.String{TypeName: "rot13adl.R13String"}.LookupByNode(nil)
    48  }
    49  func (*_R13String) LookupByIndex(idx int64) (datamodel.Node, error) {
    50  	return mixins.String{TypeName: "rot13adl.R13String"}.LookupByIndex(0)
    51  }
    52  func (*_R13String) LookupBySegment(seg datamodel.PathSegment) (datamodel.Node, error) {
    53  	return mixins.String{TypeName: "rot13adl.R13String"}.LookupBySegment(seg)
    54  }
    55  func (*_R13String) MapIterator() datamodel.MapIterator {
    56  	return nil
    57  }
    58  func (*_R13String) ListIterator() datamodel.ListIterator {
    59  	return nil
    60  }
    61  func (*_R13String) Length() int64 {
    62  	return -1
    63  }
    64  func (*_R13String) IsAbsent() bool {
    65  	return false
    66  }
    67  func (*_R13String) IsNull() bool {
    68  	return false
    69  }
    70  func (*_R13String) AsBool() (bool, error) {
    71  	return mixins.String{TypeName: "rot13adl.R13String"}.AsBool()
    72  }
    73  func (*_R13String) AsInt() (int64, error) {
    74  	return mixins.String{TypeName: "rot13adl.R13String"}.AsInt()
    75  }
    76  func (*_R13String) AsFloat() (float64, error) {
    77  	return mixins.String{TypeName: "rot13adl.R13String"}.AsFloat()
    78  }
    79  func (n *_R13String) AsString() (string, error) {
    80  	return n.synthesized, nil
    81  }
    82  func (*_R13String) AsBytes() ([]byte, error) {
    83  	return mixins.String{TypeName: "rot13adl.R13String"}.AsBytes()
    84  }
    85  func (*_R13String) AsLink() (datamodel.Link, error) {
    86  	return mixins.String{TypeName: "rot13adl.R13String"}.AsLink()
    87  }
    88  func (*_R13String) Prototype() datamodel.NodePrototype {
    89  	return _R13String__Prototype{}
    90  }
    91  
    92  // -- NodePrototype -->
    93  
    94  var _ datamodel.NodePrototype = _R13String__Prototype{}
    95  
    96  type _R13String__Prototype struct {
    97  	// There's no configuration to this ADL.
    98  
    99  	// A more complex ADL might have some kind of parameters here.
   100  	//
   101  	// The general contract of a NodePrototype is supposed to be that:
   102  	// when you get one from an existing Node,
   103  	//  it should have enough information to create a new Node that
   104  	//   could "replace" the previous one in whatever context it's in.
   105  	// For ADLs, that means it should carry most of the configuration.
   106  	//
   107  	// An ADL that does multi-block stuff might also need functions like a LinkLoader passed in through here.
   108  }
   109  
   110  func (np _R13String__Prototype) NewBuilder() datamodel.NodeBuilder {
   111  	return &_R13String__Builder{}
   112  }
   113  
   114  // -- NodeBuilder -->
   115  
   116  var _ datamodel.NodeBuilder = (*_R13String__Builder)(nil)
   117  
   118  type _R13String__Builder struct {
   119  	_R13String__Assembler
   120  }
   121  
   122  func (nb *_R13String__Builder) Build() datamodel.Node {
   123  	if nb.m != schema.Maybe_Value {
   124  		panic("invalid state: cannot call Build on an assembler that's not finished")
   125  	}
   126  	return nb.w
   127  }
   128  func (nb *_R13String__Builder) Reset() {
   129  	*nb = _R13String__Builder{}
   130  }
   131  
   132  // -- NodeAssembler -->
   133  
   134  var _ datamodel.NodeAssembler = (*_R13String__Assembler)(nil)
   135  
   136  type _R13String__Assembler struct {
   137  	w *_R13String
   138  	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.
   139  }
   140  
   141  func (_R13String__Assembler) BeginMap(sizeHint int64) (datamodel.MapAssembler, error) {
   142  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.BeginMap(0)
   143  }
   144  func (_R13String__Assembler) BeginList(sizeHint int64) (datamodel.ListAssembler, error) {
   145  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.BeginList(0)
   146  }
   147  func (na *_R13String__Assembler) AssignNull() error {
   148  	// REVIEW: unclear how this might compose with some other context (like a schema) which does allow nulls.  Probably a wrapper type?
   149  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignNull()
   150  }
   151  func (_R13String__Assembler) AssignBool(bool) error {
   152  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignBool(false)
   153  }
   154  func (_R13String__Assembler) AssignInt(int64) error {
   155  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignInt(0)
   156  }
   157  func (_R13String__Assembler) AssignFloat(float64) error {
   158  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignFloat(0)
   159  }
   160  func (na *_R13String__Assembler) AssignString(v string) error {
   161  	switch na.m {
   162  	case schema.Maybe_Value:
   163  		panic("invalid state: cannot assign into assembler that's already finished")
   164  	}
   165  	na.w = &_R13String{
   166  		raw:         rotate(v),
   167  		synthesized: v,
   168  	}
   169  	na.m = schema.Maybe_Value
   170  	return nil
   171  }
   172  func (_R13String__Assembler) AssignBytes([]byte) error {
   173  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignBytes(nil)
   174  }
   175  func (_R13String__Assembler) AssignLink(datamodel.Link) error {
   176  	return mixins.StringAssembler{TypeName: "rot13adl.R13String"}.AssignLink(nil)
   177  }
   178  func (na *_R13String__Assembler) AssignNode(v datamodel.Node) error {
   179  	if v.IsNull() {
   180  		return na.AssignNull()
   181  	}
   182  	if v2, ok := v.(*_R13String); ok {
   183  		switch na.m {
   184  		case schema.Maybe_Value:
   185  			panic("invalid state: cannot assign into assembler that's already finished")
   186  		}
   187  		na.w = v2
   188  		na.m = schema.Maybe_Value
   189  		return nil
   190  	}
   191  	if v2, err := v.AsString(); err != nil {
   192  		return err
   193  	} else {
   194  		return na.AssignString(v2)
   195  	}
   196  }
   197  func (_R13String__Assembler) Prototype() datamodel.NodePrototype {
   198  	return _R13String__Prototype{}
   199  }