github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/subnet/info.go (about) 1 package subnet 2 3 import ( 4 "github.com/TrueCloudLab/frostfs-api-go/v2/refs" 5 refsgrpc "github.com/TrueCloudLab/frostfs-api-go/v2/refs/grpc" 6 "github.com/TrueCloudLab/frostfs-api-go/v2/rpc/grpc" 7 "github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message" 8 subnet "github.com/TrueCloudLab/frostfs-api-go/v2/subnet/grpc" 9 protoutil "github.com/TrueCloudLab/frostfs-api-go/v2/util/proto" 10 ) 11 12 // Info represents information about NeoFS subnet. Structure is compatible with NeoFS API V2 protocol. 13 // 14 // Zero value represents zero subnet w/o an owner. 15 type Info struct { 16 id *refs.SubnetID 17 18 owner *refs.OwnerID 19 } 20 21 // ID returns identifier of the subnet. Nil return is equivalent to zero subnet ID. 22 func (x *Info) ID() *refs.SubnetID { 23 return x.id 24 } 25 26 // SetID returns identifier of the subnet. Nil arg is equivalent to zero subnet ID. 27 func (x *Info) SetID(id *refs.SubnetID) { 28 x.id = id 29 } 30 31 // Owner returns subnet owner's ID in NeoFS system. 32 func (x *Info) Owner() *refs.OwnerID { 33 return x.owner 34 } 35 36 // SetOwner sets subnet owner's ID in NeoFS system. 37 func (x *Info) SetOwner(id *refs.OwnerID) { 38 x.owner = id 39 } 40 41 // ToGRPCMessage forms subnet.SubnetInfo message and returns it as grpc.Message. 42 func (x *Info) ToGRPCMessage() grpc.Message { 43 var m *subnet.SubnetInfo 44 45 if x != nil { 46 m = new(subnet.SubnetInfo) 47 48 m.SetID(x.id.ToGRPCMessage().(*refsgrpc.SubnetID)) 49 m.SetOwner(x.owner.ToGRPCMessage().(*refsgrpc.OwnerID)) 50 } 51 52 return m 53 } 54 55 // FromGRPCMessage restores Info from grpc.Message. 56 // 57 // Supported types: 58 // - subnet.SubnetInfo. 59 func (x *Info) FromGRPCMessage(m grpc.Message) error { 60 v, ok := m.(*subnet.SubnetInfo) 61 if !ok { 62 return message.NewUnexpectedMessageType(m, v) 63 } 64 65 var err error 66 67 id := v.GetId() 68 if id == nil { 69 x.id = nil 70 } else { 71 if x.id == nil { 72 x.id = new(refs.SubnetID) 73 } 74 75 err = x.id.FromGRPCMessage(id) 76 if err != nil { 77 return err 78 } 79 } 80 81 ownerID := v.GetOwner() 82 if ownerID == nil { 83 x.owner = nil 84 } else { 85 if x.owner == nil { 86 x.owner = new(refs.OwnerID) 87 } 88 89 err = x.owner.FromGRPCMessage(ownerID) 90 if err != nil { 91 return err 92 } 93 } 94 95 return nil 96 } 97 98 // SubnetInfo message field numbers 99 const ( 100 _ = iota 101 subnetInfoIDFNum 102 subnetInfoOwnerFNum 103 ) 104 105 // StableMarshal marshals Info to NeoFS API V2 binary format (Protocol Buffers with direct field order). 106 // 107 // Returns a slice of recorded data. Data is written to the provided buffer if there is enough space. 108 func (x *Info) StableMarshal(buf []byte) []byte { 109 if x == nil { 110 return []byte{} 111 } 112 113 if buf == nil { 114 buf = make([]byte, x.StableSize()) 115 } 116 117 var offset int 118 119 offset += protoutil.NestedStructureMarshal(subnetInfoIDFNum, buf[offset:], x.id) 120 protoutil.NestedStructureMarshal(subnetInfoOwnerFNum, buf[offset:], x.owner) 121 122 return buf 123 } 124 125 // StableSize returns the number of bytes required to write Info in NeoFS API V2 binary format (see StableMarshal). 126 func (x *Info) StableSize() (size int) { 127 if x != nil { 128 size += protoutil.NestedStructureSize(subnetInfoIDFNum, x.id) 129 size += protoutil.NestedStructureSize(subnetInfoOwnerFNum, x.owner) 130 } 131 132 return 133 } 134 135 // Unmarshal decodes Info from NeoFS API V2 binary format (see StableMarshal). 136 func (x *Info) Unmarshal(data []byte) error { 137 return message.Unmarshal(x, data, new(subnet.SubnetInfo)) 138 }