2021-11-23 12:41:55 +00:00
|
|
|
package subnetid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-15 07:12:35 +00:00
|
|
|
"strconv"
|
2021-11-23 12:41:55 +00:00
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
2021-11-23 12:41:55 +00:00
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ID represents unique identifier of the subnet in the FrostFS network.
|
2021-11-23 12:41:55 +00:00
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// ID is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs.SubnetID
|
2022-06-15 07:12:35 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
2021-11-23 12:41:55 +00:00
|
|
|
//
|
2022-06-15 07:12:35 +00:00
|
|
|
// Instances can be created using built-in var declaration. Zero value is
|
2022-12-29 10:46:18 +00:00
|
|
|
// equivalent to identifier of the zero subnet (whole FrostFS network).
|
2022-06-15 07:12:35 +00:00
|
|
|
type ID struct {
|
|
|
|
m refs.SubnetID
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// ReadFromV2 reads ID from the refs.SubnetID message. Checks if the
|
2022-12-29 10:46:18 +00:00
|
|
|
// message conforms to FrostFS API V2 protocol.
|
2021-11-23 12:41:55 +00:00
|
|
|
//
|
2022-06-15 07:12:35 +00:00
|
|
|
// See also WriteToV2.
|
|
|
|
func (x *ID) ReadFromV2(msg refs.SubnetID) error {
|
|
|
|
x.m = msg
|
|
|
|
return nil
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// WriteToV2 writes ID to refs.SubnetID message structure. The message MUST NOT
|
|
|
|
// be nil.
|
2021-11-23 12:41:55 +00:00
|
|
|
//
|
2022-06-15 07:12:35 +00:00
|
|
|
// See also ReadFromV2.
|
|
|
|
func (x ID) WriteToV2(msg *refs.SubnetID) {
|
|
|
|
*msg = x.m
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// Equals defines a comparison relation between two ID instances.
|
2021-11-23 12:41:55 +00:00
|
|
|
//
|
2022-06-15 07:12:35 +00:00
|
|
|
// Note that comparison using '==' operator is not recommended since it MAY result
|
|
|
|
// in loss of compatibility.
|
|
|
|
func (x ID) Equals(x2 ID) bool {
|
|
|
|
return x.m.GetValue() == x2.m.GetValue()
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// EncodeToString encodes ID into FrostFS API protocol string (base10 encoding).
|
2021-11-23 12:41:55 +00:00
|
|
|
//
|
2022-06-15 07:12:35 +00:00
|
|
|
// See also DecodeString.
|
|
|
|
func (x ID) EncodeToString() string {
|
|
|
|
return strconv.FormatUint(uint64(x.m.GetValue()), 10)
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// DecodeString decodes string calculated using EncodeToString. Returns
|
|
|
|
// an error if s is malformed.
|
|
|
|
func (x *ID) DecodeString(s string) error {
|
|
|
|
num, err := strconv.ParseUint(s, 10, 32)
|
2021-11-23 12:41:55 +00:00
|
|
|
if err != nil {
|
2022-06-15 07:12:35 +00:00
|
|
|
return fmt.Errorf("invalid numeric value: %w", err)
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
x.m.SetValue(uint32(num))
|
|
|
|
|
|
|
|
return nil
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// String implements fmt.Stringer.
|
|
|
|
//
|
|
|
|
// String is designed to be human-readable, and its format MAY differ between
|
|
|
|
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
|
2022-12-29 10:46:18 +00:00
|
|
|
// be used to encode ID into FrostFS protocol string.
|
2022-06-15 07:12:35 +00:00
|
|
|
func (x ID) String() string {
|
|
|
|
return "#" + strconv.FormatUint(uint64(x.m.GetValue()), 10)
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Marshal encodes ID into a binary format of the FrostFS API protocol
|
2022-06-15 07:12:35 +00:00
|
|
|
// (Protocol Buffers with direct field order).
|
2021-11-23 12:41:55 +00:00
|
|
|
//
|
2022-06-15 07:12:35 +00:00
|
|
|
// See also Unmarshal.
|
|
|
|
func (x ID) Marshal() []byte {
|
|
|
|
return x.m.StableMarshal(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal decodes binary ID calculated using Marshal. Returns an error
|
|
|
|
// describing a format violation.
|
2021-11-23 12:41:55 +00:00
|
|
|
func (x *ID) Unmarshal(data []byte) error {
|
2022-06-15 07:12:35 +00:00
|
|
|
return x.m.Unmarshal(data)
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// SetNumeric sets ID value in numeric format. By default, number is 0 which
|
|
|
|
// refers to the zero subnet.
|
|
|
|
func (x *ID) SetNumeric(num uint32) {
|
|
|
|
x.m.SetValue(num)
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// IsZero compares id with zero subnet ID.
|
2021-11-23 12:41:55 +00:00
|
|
|
func IsZero(id ID) bool {
|
2022-06-15 07:12:35 +00:00
|
|
|
return id.Equals(ID{})
|
2021-11-23 12:41:55 +00:00
|
|
|
}
|
2021-11-23 18:17:58 +00:00
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
// MakeZero makes ID to refer to zero subnet.
|
2021-11-23 18:17:58 +00:00
|
|
|
//
|
|
|
|
// Makes no sense to call on zero value (e.g. declared using var).
|
|
|
|
func MakeZero(id *ID) {
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(0)
|
2021-11-23 18:17:58 +00:00
|
|
|
}
|