Skip to content

decimal: add decomposer interface #141

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

Merged
merged 1 commit into from
Sep 5, 2019

Conversation

kardianos
Copy link
Contributor

@kardianos
Copy link
Contributor Author

Please let me know what you think of this idea. See referenced issue.

S string // String value.
E bool // Expect an error.
}{
{N: "Normal-1", S: "123.456"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these test cases include expected results which are then tested against the actual results with assert.Equal? It would be good to have more test cases as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any uses of an "assert" package, but, I added more test cases, it already checks with Decimal.Cmp and I added a check to the original S. Because this is a round trip test, I'm checking the final decimal against the original parsed number.

// Compose sets the internal decimal value from parts. If the value cannot be
// represented then an error should be returned.
func (d *Decimal) Compose(form byte, negative bool, coefficient []byte, exponent int32) error {
switch form {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These forms should constants rather than magic numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added consts above. The interface is designed to be a byte type to allow it to not rely on an external package for types.

case 0:
// Set rest of finite form below.
case 1:
return fmt.Errorf("Infinite form not supported")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why even include these cases, or have the form argument at all, if they cannot be accepted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This interface is designed to convert between many different decimal packages. Some of these other packages handle NaN and and Infinite forms. This is to have explicit error messages for messaging.

@kardianos kardianos force-pushed the kardianos-decomposer branch 2 times, most recently from e8157b0 to a866c9d Compare April 23, 2019 17:38
@kardianos
Copy link
Contributor Author

I removed the use of testing.T.Run, which was introduced in Go1.7. CI passes now.

@kardianos
Copy link
Contributor Author

Ping.

@kardianos kardianos force-pushed the kardianos-decomposer branch from a866c9d to 140db38 Compare May 4, 2019 07:16
@kardianos
Copy link
Contributor Author

Ping

@kardianos
Copy link
Contributor Author

@njason Can we resume this conversation? This is in Go1.13 and crdb/apd and ericlagergren/decimal.

@njason
Copy link
Member

njason commented Sep 3, 2019

@kardianos I see this has been added as an experimental interface to 1.13 which is in an unstable release state. Please circle back when this is in a stable release.

@kardianos
Copy link
Contributor Author

1.13 has now been released.

@njason
Copy link
Member

njason commented Sep 4, 2019

Can we remove the decomposer interface in this PR since it's already defined in core?

@kardianos kardianos force-pushed the kardianos-decomposer branch from 140db38 to fe30a7b Compare September 4, 2019 22:19
@kardianos kardianos force-pushed the kardianos-decomposer branch from fe30a7b to 75bb2cb Compare September 4, 2019 22:36
@kardianos
Copy link
Contributor Author

The most recent build on TravisCI complained about Go1.2 being missing. Looking on https://golang.org/dl/ Go 1.2.2 is present, but Go1.2 is indeed gone. To test, I changed the travis ci file and it passed using Go1.2.2.

@kardianos
Copy link
Contributor Author

Also, I removed the interface per your request.

@njason njason merged commit a36b5d8 into shopspring:master Sep 5, 2019
@5teven
Copy link

5teven commented Sep 11, 2019

Hello everyone

After this update i got error:
pq: encode: unknown type for decimal.Decimal

I'm using:
go1.13 linux/amd64
postgresql 10
sqlx

@kardianos
Copy link
Contributor Author

@5teven What is the postgresql driver version? Also, please open a new issue at golang.org/issue and @ me.

@tietang
Copy link

tietang commented Nov 23, 2019

After this update i got error:
cannot convert type: decimal.Decimal

go 1.13.4
go-sql-driver/mysql 1.4.1

i think go mysql driver can't support decimalDecompose.

please see:

  1. database/sql/driver.IsValue, line 176:
    // IsValue reports whether v is a valid Value parameter type. func IsValue(v interface{}) bool { if v == nil { return true } switch v.(type) { case []byte, bool, float64, int64, string, time.Time: return true case decimalDecompose: return true } return false }

2.go-sql-driver/mysql statement.go line 140, method ConvertValue:
line 141:
`
if driver.IsValue(v) {
return v, nil
}

if vr, ok := v.(driver.Valuer); ok {
	sv, err := callValuerValue(vr)
	if err != nil {
		return nil, err
	}
	if !driver.IsValue(sv) {
		return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
	}
	return sv, nil
}

`

  1. go-sql-driver/mysql packets.go line 1071

line 1071
default: return fmt.Errorf("cannot convert type: %T", arg) }

@njason
Copy link
Member

njason commented Nov 25, 2019

commit 75bb2cb has been reverted from master and will be worked on in another branch

fairyhunter13 added a commit to fairyhunter13/decimal that referenced this pull request Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants