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

     1  package mixins
     2  
     3  import (
     4  	"github.com/ipld/go-ipld-prime/datamodel"
     5  )
     6  
     7  // Bytes can be embedded in a struct to provide all the methods that
     8  // have fixed output for any int-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 Bytes struct {
    19  	TypeName string
    20  }
    21  
    22  func (Bytes) Kind() datamodel.Kind {
    23  	return datamodel.Kind_Bytes
    24  }
    25  func (x Bytes) LookupByString(string) (datamodel.Node, error) {
    26  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByString", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_Bytes}
    27  }
    28  func (x Bytes) LookupByNode(key datamodel.Node) (datamodel.Node, error) {
    29  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByNode", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_Bytes}
    30  }
    31  func (x Bytes) LookupByIndex(idx int64) (datamodel.Node, error) {
    32  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByIndex", AppropriateKind: datamodel.KindSet_JustList, ActualKind: datamodel.Kind_Bytes}
    33  }
    34  func (x Bytes) LookupBySegment(datamodel.PathSegment) (datamodel.Node, error) {
    35  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupBySegment", AppropriateKind: datamodel.KindSet_Recursive, ActualKind: datamodel.Kind_Bytes}
    36  }
    37  func (Bytes) MapIterator() datamodel.MapIterator {
    38  	return nil
    39  }
    40  func (Bytes) ListIterator() datamodel.ListIterator {
    41  	return nil
    42  }
    43  func (Bytes) Length() int64 {
    44  	return -1
    45  }
    46  func (Bytes) IsAbsent() bool {
    47  	return false
    48  }
    49  func (Bytes) IsNull() bool {
    50  	return false
    51  }
    52  func (x Bytes) AsBool() (bool, error) {
    53  	return false, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsBool", AppropriateKind: datamodel.KindSet_JustBool, ActualKind: datamodel.Kind_Bytes}
    54  }
    55  func (x Bytes) AsInt() (int64, error) {
    56  	return 0, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsInt", AppropriateKind: datamodel.KindSet_JustInt, ActualKind: datamodel.Kind_Bytes}
    57  }
    58  func (x Bytes) AsFloat() (float64, error) {
    59  	return 0, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsFloat", AppropriateKind: datamodel.KindSet_JustFloat, ActualKind: datamodel.Kind_Bytes}
    60  }
    61  func (x Bytes) AsString() (string, error) {
    62  	return "", datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsString", AppropriateKind: datamodel.KindSet_JustString, ActualKind: datamodel.Kind_Bytes}
    63  }
    64  func (x Bytes) AsLink() (datamodel.Link, error) {
    65  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsLink", AppropriateKind: datamodel.KindSet_JustLink, ActualKind: datamodel.Kind_Bytes}
    66  }
    67  
    68  // BytesAssembler has similar purpose as Bytes, but for (you guessed it)
    69  // the NodeAssembler interface rather than the Node interface.
    70  type BytesAssembler struct {
    71  	TypeName string
    72  }
    73  
    74  func (x BytesAssembler) BeginMap(sizeHint int64) (datamodel.MapAssembler, error) {
    75  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginMap", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_Bytes}
    76  }
    77  func (x BytesAssembler) BeginList(sizeHint int64) (datamodel.ListAssembler, error) {
    78  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginList", AppropriateKind: datamodel.KindSet_JustList, ActualKind: datamodel.Kind_Bytes}
    79  }
    80  func (x BytesAssembler) AssignNull() error {
    81  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignNull", AppropriateKind: datamodel.KindSet_JustNull, ActualKind: datamodel.Kind_Bytes}
    82  }
    83  func (x BytesAssembler) AssignBool(bool) error {
    84  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBool", AppropriateKind: datamodel.KindSet_JustBool, ActualKind: datamodel.Kind_Bytes}
    85  }
    86  func (x BytesAssembler) AssignInt(int64) error {
    87  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignInt", AppropriateKind: datamodel.KindSet_JustInt, ActualKind: datamodel.Kind_Bytes}
    88  }
    89  func (x BytesAssembler) AssignFloat(float64) error {
    90  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignFloat", AppropriateKind: datamodel.KindSet_JustFloat, ActualKind: datamodel.Kind_Bytes}
    91  }
    92  func (x BytesAssembler) AssignString(string) error {
    93  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignString", AppropriateKind: datamodel.KindSet_JustString, ActualKind: datamodel.Kind_Bytes}
    94  }
    95  func (x BytesAssembler) AssignLink(datamodel.Link) error {
    96  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignLink", AppropriateKind: datamodel.KindSet_JustLink, ActualKind: datamodel.Kind_Bytes}
    97  }