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