-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
types: Dec.MarshalJSON now marshals as a normal decimal string #2506
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20cc13e
types: Dec.MarshalJSON now marshals as a normal decimal string
ValarDragon 8d59b51
fix the weird non-alphanumeric key issue
ValarDragon 73bfd6c
Merge branch 'develop' into dev/change_dec_marshaljson
ValarDragon c2d6892
Switch to new Decimal.String() implementation
ValarDragon 864ca4e
Merge branch 'dev/change_dec_marshaljson' of github.com:cosmos/cosmos…
ValarDragon 1a463eb
make more clear
ValarDragon 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
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 |
---|---|---|
|
@@ -248,29 +248,32 @@ func (d Dec) QuoInt(i Int) Dec { | |
} | ||
|
||
func (d Dec) String() string { | ||
str := d.ToLeftPaddedWithDecimals(Precision) | ||
placement := len(str) - Precision | ||
if placement < 0 { | ||
panic("too few decimal digits") | ||
bz, err := d.Int.MarshalText() | ||
if err != nil { | ||
return "" | ||
} | ||
var bzWDec []byte | ||
// TODO: Remove trailing zeros | ||
// case 1, purely decimal | ||
if len(bz) <= 10 { | ||
bzWDec = make([]byte, 12) | ||
// 0. prefix | ||
bzWDec[0] = byte('0') | ||
bzWDec[1] = byte('.') | ||
// set relevant digits to 0 | ||
for i := 0; i < 10-len(bz); i++ { | ||
bzWDec[i+2] = byte('0') | ||
} | ||
// set last few digits | ||
copy(bzWDec[2+(10-len(bz)):], bz) | ||
} else { | ||
// len(bz) + 1 to account for the decimal point that is being added | ||
bzWDec = make([]byte, len(bz)+1) | ||
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. Store |
||
copy(bzWDec, bz[:len(bz)-10]) | ||
bzWDec[len(bz)-10] = byte('.') | ||
copy(bzWDec[len(bz)-9:], bz[len(bz)-10:]) | ||
} | ||
return str[:placement] + "." + str[placement:] | ||
} | ||
|
||
// TODO panic if negative or if totalDigits < len(initStr)??? | ||
// evaluate as an integer and return left padded string | ||
func (d Dec) ToLeftPaddedWithDecimals(totalDigits int8) string { | ||
intStr := d.Int.String() | ||
fcode := `%0` + strconv.Itoa(int(totalDigits)) + `s` | ||
return fmt.Sprintf(fcode, intStr) | ||
} | ||
|
||
// TODO panic if negative or if totalDigits < len(initStr)??? | ||
// evaluate as an integer and return left padded string | ||
func (d Dec) ToLeftPadded(totalDigits int8) string { | ||
chopped := chopPrecisionAndRoundNonMutative(d.Int) | ||
intStr := chopped.String() | ||
fcode := `%0` + strconv.Itoa(int(totalDigits)) + `s` | ||
return fmt.Sprintf(fcode, intStr) | ||
return string(bzWDec) | ||
} | ||
|
||
// ____ | ||
|
@@ -407,17 +410,13 @@ func (d *Dec) UnmarshalAmino(text string) (err error) { | |
return nil | ||
} | ||
|
||
// MarshalJSON defines custom encoding scheme | ||
// MarshalJSON marshals the decimal | ||
func (d Dec) MarshalJSON() ([]byte, error) { | ||
if d.Int == nil { | ||
return nilJSON, nil | ||
} | ||
|
||
bz, err := d.Int.MarshalText() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return json.Marshal(string(bz)) | ||
return json.Marshal(d.String()) | ||
} | ||
|
||
// UnmarshalJSON defines custom decoding scheme | ||
|
@@ -431,7 +430,13 @@ func (d *Dec) UnmarshalJSON(bz []byte) error { | |
if err != nil { | ||
return err | ||
} | ||
return d.Int.UnmarshalText([]byte(text)) | ||
// TODO: Reuse dec allocation | ||
newDec, err := NewDecFromStr(text) | ||
if err != nil { | ||
return err | ||
} | ||
d.Int = newDec.Int | ||
return nil | ||
} | ||
|
||
//___________________________________________________________________________________ | ||
|
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
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.
Store
len(bz)
in a variable