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