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