github.com/ipld/go-ipld-prime@v0.21.0/node/mixins/string.go (about)

     1  package mixins
     2  
     3  import (
     4  	"github.com/ipld/go-ipld-prime/datamodel"
     5  )
     6  
     7  // String can be embedded in a struct to provide all the methods that
     8  // have fixed output for any string-kinded nodes.
     9  // (Mostly this includes all the methods which simply return ErrWrongKind.)
    10  // Other methods will still need to be implemented to finish conforming to Node.
    11  //
    12  // To conserve memory and get a TypeName in errors without embedding,
    13  // write methods on your type with a body that simply initializes this struct
    14  // and immediately uses the relevant method;
    15  // this is more verbose in source, but compiles to a tighter result:
    16  // in memory, there's no embed; and in runtime, the calls will be inlined
    17  // and thus have no cost in execution time.
    18  type String struct {
    19  	TypeName string
    20  }
    21  
    22  func (String) Kind() datamodel.Kind {
    23  	return datamodel.Kind_String
    24  }
    25  func (x String) LookupByString(string) (datamodel.Node, error) {
    26  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByString", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_String}
    27  }
    28  func (x String) LookupByNode(key datamodel.Node) (datamodel.Node, error) {
    29  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByNode", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_String}
    30  }
    31  func (x String) LookupByIndex(idx int64) (datamodel.Node, error) {
    32  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByIndex", AppropriateKind: datamodel.KindSet_JustList, ActualKind: datamodel.Kind_String}
    33  }
    34  func (x String) LookupBySegment(datamodel.PathSegment) (datamodel.Node, error) {
    35  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupBySegment", AppropriateKind: datamodel.KindSet_Recursive, ActualKind: datamodel.Kind_String}
    36  }
    37  func (String) MapIterator() datamodel.MapIterator {
    38  	return nil
    39  }
    40  func (String) ListIterator() datamodel.ListIterator {
    41  	return nil
    42  }
    43  func (String) Length() int64 {
    44  	return -1
    45  }
    46  func (String) IsAbsent() bool {
    47  	return false
    48  }
    49  func (String) IsNull() bool {
    50  	return false
    51  }
    52  func (x String) AsBool() (bool, error) {
    53  	return false, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsBool", AppropriateKind: datamodel.KindSet_JustBool, ActualKind: datamodel.Kind_String}
    54  }
    55  func (x String) AsInt() (int64, error) {
    56  	return 0, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsInt", AppropriateKind: datamodel.KindSet_JustInt, ActualKind: datamodel.Kind_String}
    57  }
    58  func (x String) AsFloat() (float64, error) {
    59  	return 0, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsFloat", AppropriateKind: datamodel.KindSet_JustFloat, ActualKind: datamodel.Kind_String}
    60  }
    61  func (x String) AsBytes() ([]byte, error) {
    62  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsBytes", AppropriateKind: datamodel.KindSet_JustBytes, ActualKind: datamodel.Kind_String}
    63  }
    64  func (x String) AsLink() (datamodel.Link, error) {
    65  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsLink", AppropriateKind: datamodel.KindSet_JustLink, ActualKind: datamodel.Kind_String}
    66  }
    67  
    68  // StringAssembler has similar purpose as String, but for (you guessed it)
    69  // the NodeAssembler interface rather than the Node interface.
    70  type StringAssembler struct {
    71  	TypeName string
    72  }
    73  
    74  func (x StringAssembler) BeginMap(sizeHint int64) (datamodel.MapAssembler, error) {
    75  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginMap", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_String}
    76  }
    77  func (x StringAssembler) BeginList(sizeHint int64) (datamodel.ListAssembler, error) {
    78  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginList", AppropriateKind: datamodel.KindSet_JustList, ActualKind: datamodel.Kind_String}
    79  }
    80  func (x StringAssembler) AssignNull() error {
    81  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignNull", AppropriateKind: datamodel.KindSet_JustNull, ActualKind: datamodel.Kind_String}
    82  }
    83  func (x StringAssembler) AssignBool(bool) error {
    84  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBool", AppropriateKind: datamodel.KindSet_JustBool, ActualKind: datamodel.Kind_String}
    85  }
    86  func (x StringAssembler) AssignInt(int64) error {
    87  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignInt", AppropriateKind: datamodel.KindSet_JustInt, ActualKind: datamodel.Kind_String}
    88  }
    89  func (x StringAssembler) AssignFloat(float64) error {
    90  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignFloat", AppropriateKind: datamodel.KindSet_JustFloat, ActualKind: datamodel.Kind_String}
    91  }
    92  func (x StringAssembler) AssignBytes([]byte) error {
    93  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBytes", AppropriateKind: datamodel.KindSet_JustBytes, ActualKind: datamodel.Kind_String}
    94  }
    95  func (x StringAssembler) AssignLink(datamodel.Link) error {
    96  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignLink", AppropriateKind: datamodel.KindSet_JustLink, ActualKind: datamodel.Kind_String}
    97  }