2020-08-25 11:44:21 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
)
|
|
|
|
|
2020-09-10 11:29:11 +00:00
|
|
|
// Version represents v2-compatible version.
|
|
|
|
type Version refs.Version
|
|
|
|
|
2021-06-08 18:37:20 +00:00
|
|
|
const sdkMjr, sdkMnr = 2, 7
|
2020-09-10 11:29:11 +00:00
|
|
|
|
|
|
|
// NewVersionFromV2 wraps v2 Version message to Version.
|
2021-06-08 18:22:33 +00:00
|
|
|
//
|
|
|
|
// Nil refs.Version converts to nil.
|
2020-09-10 11:29:11 +00:00
|
|
|
func NewVersionFromV2(v *refs.Version) *Version {
|
|
|
|
return (*Version)(v)
|
|
|
|
}
|
2020-08-25 11:44:21 +00:00
|
|
|
|
2020-09-10 11:29:11 +00:00
|
|
|
// NewVersion creates and initializes blank Version.
|
|
|
|
//
|
|
|
|
// Works similar as NewVersionFromV2(new(Version)).
|
2021-06-08 18:25:35 +00:00
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - major: 0;
|
|
|
|
// - minor: 0.
|
2020-09-10 11:29:11 +00:00
|
|
|
func NewVersion() *Version {
|
|
|
|
return NewVersionFromV2(new(refs.Version))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SDKVersion returns Version instance that
|
|
|
|
// initialized to current SDK revision number.
|
|
|
|
func SDKVersion() *Version {
|
|
|
|
v := NewVersion()
|
|
|
|
v.SetMajor(sdkMjr)
|
|
|
|
v.SetMinor(sdkMnr)
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
2020-08-25 11:44:21 +00:00
|
|
|
|
2020-11-16 08:08:47 +00:00
|
|
|
// Major returns major number of the revision.
|
|
|
|
func (v *Version) Major() uint32 {
|
2020-09-10 11:29:11 +00:00
|
|
|
return (*refs.Version)(v).
|
|
|
|
GetMajor()
|
2020-08-25 11:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 11:29:11 +00:00
|
|
|
// SetMajor sets major number of the revision.
|
|
|
|
func (v *Version) SetMajor(val uint32) {
|
|
|
|
(*refs.Version)(v).
|
|
|
|
SetMajor(val)
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:08:47 +00:00
|
|
|
// Minor returns minor number of the revision.
|
|
|
|
func (v *Version) Minor() uint32 {
|
2020-09-10 11:29:11 +00:00
|
|
|
return (*refs.Version)(v).
|
|
|
|
GetMinor()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMinor sets minor number of the revision.
|
|
|
|
func (v *Version) SetMinor(val uint32) {
|
|
|
|
(*refs.Version)(v).
|
|
|
|
SetMinor(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts Version to v2 Version message.
|
2021-06-08 18:22:33 +00:00
|
|
|
//
|
|
|
|
// Nil Version converts to nil.
|
2020-09-10 11:29:11 +00:00
|
|
|
func (v *Version) ToV2() *refs.Version {
|
|
|
|
return (*refs.Version)(v)
|
|
|
|
}
|
2020-08-25 11:44:21 +00:00
|
|
|
|
2020-09-10 11:29:11 +00:00
|
|
|
func (v *Version) String() string {
|
2020-11-16 08:08:47 +00:00
|
|
|
return fmt.Sprintf("v%d.%d", v.Major(), v.Minor())
|
2020-08-25 11:44:21 +00:00
|
|
|
}
|
2020-11-03 08:29:41 +00:00
|
|
|
|
|
|
|
// IsSupportedVersion returns error if v is not supported by current SDK.
|
|
|
|
func IsSupportedVersion(v *Version) error {
|
2021-03-17 11:20:04 +00:00
|
|
|
mjr, mnr := v.Major(), v.Minor()
|
|
|
|
|
|
|
|
if mjr != 2 || mnr > sdkMnr {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("unsupported version %d.%d", mjr, mnr)
|
2020-11-03 08:29:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 11:20:04 +00:00
|
|
|
return nil
|
2020-11-03 08:29:41 +00:00
|
|
|
}
|
2020-11-13 13:19:16 +00:00
|
|
|
|
|
|
|
// Marshal marshals Version into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (v *Version) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*refs.Version)(v).
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Version.
|
|
|
|
func (v *Version) Unmarshal(data []byte) error {
|
|
|
|
return (*refs.Version)(v).
|
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Version to protobuf JSON format.
|
|
|
|
func (v *Version) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*refs.Version)(v).
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Version from protobuf JSON format.
|
|
|
|
func (v *Version) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*refs.Version)(v).
|
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|