-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: Replace github.com/nspcc-dev/neofs-api-go/v2
module
#667
Draft
cthulhu-rider
wants to merge
4
commits into
master
Choose a base branch
from
avoid-apigo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,41 @@ | ||
package accounting | ||
|
||
import ( | ||
"github.com/nspcc-dev/neofs-api-go/v2/accounting" | ||
neofsproto "github.com/nspcc-dev/neofs-sdk-go/internal/proto" | ||
protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// Decimal represents decimal number for accounting operations. | ||
// | ||
// Decimal is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/accounting.Decimal | ||
// message. See ReadFromV2 / WriteToV2 methods. | ||
// Decimal is mutually compatible with [protoaccounting.Decimal] message. See | ||
// [Decimal.FromProtoMessage] / [Decimal.FromProtoMessage] methods. | ||
// | ||
// Instances can be created using built-in var declaration. | ||
// | ||
// Note that direct typecast is not safe and may result in loss of compatibility: | ||
// | ||
// _ = Decimal(accounting.Decimal{}) // not recommended | ||
type Decimal accounting.Decimal | ||
type Decimal struct { | ||
val int64 | ||
prec uint32 | ||
} | ||
|
||
// ReadFromV2 reads Decimal from the accounting.Decimal message. Checks if the | ||
// message conforms to NeoFS API V2 protocol. | ||
// FromProtoMessage validates m according to the NeoFS API protocol and restores | ||
// d from it. | ||
// | ||
// See also WriteToV2. | ||
func (d *Decimal) ReadFromV2(m accounting.Decimal) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that temporary |
||
*d = Decimal(m) | ||
// See also [Decimal.ProtoMessage]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this, |
||
func (d *Decimal) FromProtoMessage(m *protoaccounting.Decimal) error { | ||
d.val = m.Value | ||
d.prec = m.Precision | ||
return nil | ||
} | ||
|
||
// WriteToV2 writes Decimal to the accounting.Decimal message. | ||
// The message must not be nil. | ||
// ProtoMessage converts d into message to transmit using the NeoFS API | ||
// protocol. | ||
// | ||
// See also ReadFromV2. | ||
func (d Decimal) WriteToV2(m *accounting.Decimal) { | ||
*m = (accounting.Decimal)(d) | ||
// See also [Decimal.FromProtoMessage]. | ||
func (d Decimal) ProtoMessage() *protoaccounting.Decimal { | ||
return &protoaccounting.Decimal{ | ||
Value: d.val, | ||
Precision: d.prec, | ||
} | ||
} | ||
|
||
// Value returns value of the decimal number. | ||
|
@@ -39,14 +44,14 @@ func (d Decimal) WriteToV2(m *accounting.Decimal) { | |
// | ||
// See also SetValue. | ||
func (d Decimal) Value() int64 { | ||
return (*accounting.Decimal)(&d).GetValue() | ||
return d.val | ||
} | ||
|
||
// SetValue sets value of the decimal number. | ||
// | ||
// See also Value. | ||
func (d *Decimal) SetValue(v int64) { | ||
(*accounting.Decimal)(d).SetValue(v) | ||
d.val = v | ||
} | ||
|
||
// Precision returns precision of the decimal number. | ||
|
@@ -55,25 +60,22 @@ func (d *Decimal) SetValue(v int64) { | |
// | ||
// See also SetPrecision. | ||
func (d Decimal) Precision() uint32 { | ||
return (*accounting.Decimal)(&d).GetPrecision() | ||
return d.prec | ||
} | ||
|
||
// SetPrecision sets precision of the decimal number. | ||
// | ||
// See also Precision. | ||
func (d *Decimal) SetPrecision(p uint32) { | ||
(*accounting.Decimal)(d).SetPrecision(p) | ||
d.prec = p | ||
} | ||
|
||
// Marshal encodes Decimal into a binary format of the NeoFS API protocol | ||
// (Protocol Buffers with direct field order). | ||
// | ||
// See also Unmarshal. | ||
func (d Decimal) Marshal() []byte { | ||
var m accounting.Decimal | ||
d.WriteToV2(&m) | ||
|
||
return m.StableMarshal(nil) | ||
return neofsproto.MarshalMessage(d.ProtoMessage()) | ||
} | ||
|
||
// Unmarshal decodes NeoFS API protocol binary format into the Decimal | ||
|
@@ -82,12 +84,12 @@ func (d Decimal) Marshal() []byte { | |
// | ||
// See also Marshal. | ||
func (d *Decimal) Unmarshal(data []byte) error { | ||
var m accounting.Decimal | ||
var m protoaccounting.Decimal | ||
|
||
err := m.Unmarshal(data) | ||
err := proto.Unmarshal(data, &m) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return d.ReadFromV2(m) | ||
return d.FromProtoMessage(&m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest to drop such statements since regular user is not interested in this
@roman-khimov @carpawell