forked from TrueCloudLab/frostfs-api-go
[#356] refs: Implement text encoding of SubnetID
Implement `encoding.TextMarshaler` / `encoding.TextUnmarshaler` interfaces on `SubnetID` according to NeoFS API V2 protocol. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
f0af5ce759
commit
0e6e0e4678
2 changed files with 62 additions and 0 deletions
|
@ -1,5 +1,10 @@
|
|||
package refs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type OwnerID struct {
|
||||
val []byte
|
||||
}
|
||||
|
@ -183,6 +188,33 @@ func (s *SubnetID) GetValue() uint32 {
|
|||
return 0
|
||||
}
|
||||
|
||||
// MarshalText encodes SubnetID into text format according to NeoFS API V2 protocol:
|
||||
// value in base-10 integer string format.
|
||||
//
|
||||
// Implements encoding.TextMarshaler.
|
||||
func (s *SubnetID) MarshalText() ([]byte, error) {
|
||||
num := s.GetValue() // NPE safe, returns zero on nil (zero subnet)
|
||||
|
||||
return []byte(strconv.FormatUint(uint64(num), 10)), nil
|
||||
}
|
||||
|
||||
// UnmarshalText decodes SubnetID from the text according to NeoFS API V2 protocol:
|
||||
// should be base-10 integer string format.
|
||||
//
|
||||
// Must not be called on nil.
|
||||
//
|
||||
// Implements encoding.TextUnmarshaler.
|
||||
func (s *SubnetID) UnmarshalText(txt []byte) error {
|
||||
num, err := strconv.ParseUint(string(txt), 10, 32)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid numeric value: %w", err)
|
||||
}
|
||||
|
||||
s.value = uint32(num)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsZeroSubnet returns true iff the SubnetID refers to zero subnet.
|
||||
func IsZeroSubnet(id *SubnetID) bool {
|
||||
return id.GetValue() == 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue