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

     1  package mixins
     2  
     3  import (
     4  	"github.com/ipld/go-ipld-prime/datamodel"
     5  )
     6  
     7  // List 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 List struct {
    19  	TypeName string
    20  }
    21  
    22  func (List) Kind() datamodel.Kind {
    23  	return datamodel.Kind_List
    24  }
    25  func (x List) LookupByString(string) (datamodel.Node, error) {
    26  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByString", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_List}
    27  }
    28  func (x List) LookupByNode(key datamodel.Node) (datamodel.Node, error) {
    29  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByNode", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_List}
    30  }
    31  func (List) MapIterator() datamodel.MapIterator {
    32  	return nil
    33  }
    34  func (List) IsAbsent() bool {
    35  	return false
    36  }
    37  func (List) IsNull() bool {
    38  	return false
    39  }
    40  func (x List) AsBool() (bool, error) {
    41  	return false, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsBool", AppropriateKind: datamodel.KindSet_JustBool, ActualKind: datamodel.Kind_List}
    42  }
    43  func (x List) AsInt() (int64, error) {
    44  	return 0, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsInt", AppropriateKind: datamodel.KindSet_JustInt, ActualKind: datamodel.Kind_List}
    45  }
    46  func (x List) AsFloat() (float64, error) {
    47  	return 0, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsFloat", AppropriateKind: datamodel.KindSet_JustFloat, ActualKind: datamodel.Kind_List}
    48  }
    49  func (x List) AsString() (string, error) {
    50  	return "", datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsString", AppropriateKind: datamodel.KindSet_JustString, ActualKind: datamodel.Kind_List}
    51  }
    52  func (x List) AsBytes() ([]byte, error) {
    53  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsBytes", AppropriateKind: datamodel.KindSet_JustBytes, ActualKind: datamodel.Kind_List}
    54  }
    55  func (x List) AsLink() (datamodel.Link, error) {
    56  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsLink", AppropriateKind: datamodel.KindSet_JustLink, ActualKind: datamodel.Kind_List}
    57  }
    58  
    59  // ListAssembler has similar purpose as List, but for (you guessed it)
    60  // the NodeAssembler interface rather than the Node interface.
    61  type ListAssembler struct {
    62  	TypeName string
    63  }
    64  
    65  func (x ListAssembler) BeginMap(sizeHint int64) (datamodel.MapAssembler, error) {
    66  	return nil, datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginMap", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: datamodel.Kind_List}
    67  }
    68  func (x ListAssembler) AssignNull() error {
    69  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignNull", AppropriateKind: datamodel.KindSet_JustNull, ActualKind: datamodel.Kind_List}
    70  }
    71  func (x ListAssembler) AssignBool(bool) error {
    72  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBool", AppropriateKind: datamodel.KindSet_JustBool, ActualKind: datamodel.Kind_List}
    73  }
    74  func (x ListAssembler) AssignInt(int64) error {
    75  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignInt", AppropriateKind: datamodel.KindSet_JustInt, ActualKind: datamodel.Kind_List}
    76  }
    77  func (x ListAssembler) AssignFloat(float64) error {
    78  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignFloat", AppropriateKind: datamodel.KindSet_JustFloat, ActualKind: datamodel.Kind_List}
    79  }
    80  func (x ListAssembler) AssignString(string) error {
    81  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignString", AppropriateKind: datamodel.KindSet_JustString, ActualKind: datamodel.Kind_List}
    82  }
    83  func (x ListAssembler) AssignBytes([]byte) error {
    84  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBytes", AppropriateKind: datamodel.KindSet_JustBytes, ActualKind: datamodel.Kind_List}
    85  }
    86  func (x ListAssembler) AssignLink(datamodel.Link) error {
    87  	return datamodel.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignLink", AppropriateKind: datamodel.KindSet_JustLink, ActualKind: datamodel.Kind_List}
    88  }