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