Skip to content
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

CASSGO-38 Marshal error messages enhancement #1838

Merged
merged 1 commit into from
Jan 31, 2025

Conversation

tengu-alt
Copy link
Contributor

This PR provides marshal/unmarshal error messages enhancement.
According to #671 errors were updated by the supported types list.

@tengu-alt tengu-alt force-pushed the marshal-errors-enhancement branch from bb0dab6 to c3dcd98 Compare October 30, 2024 12:02
marshal.go Outdated
@@ -333,7 +333,7 @@ func marshalVarchar(info TypeInfo, value interface{}) ([]byte, error) {
case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
return rv.Bytes(), nil
}
return nil, marshalErrorf("can not marshal %T into %s", value, info)
return nil, marshalErrorf("can not marshal %T into %s. Accepted types: Marshaler, string, []byte, unsetColumn.", value, info)
Copy link
Contributor

Choose a reason for hiding this comment

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

unsetColumn is not an exported type, so the driver doesn't have to expose it to the user. There is a UnsetValue global var of type unsetColumn which is meant to be used to ignore writing. You can find it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, refactored

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant to change unsetColumn to UnsetValue, not to delete it at all

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 see, fixed.

marshal.go Outdated
@@ -2280,7 +2280,7 @@ func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
}

if k.Kind() != reflect.Struct || !k.IsValid() {
return nil, marshalErrorf("cannot marshal %T into %s", value, info)
return nil, marshalErrorf("cannot marshal %T into %s. Accepted types: Marshaler, unsetColumn, UDTMarshaler, UDTMarshaler, map[string]interface{}, struct.", value, info)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here UDTMarshaler is duplicated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

@tengu-alt tengu-alt force-pushed the marshal-errors-enhancement branch 3 times, most recently from f4a0127 to dbec58a Compare October 31, 2024 12:50
@tengu-alt tengu-alt force-pushed the marshal-errors-enhancement branch from dbec58a to d6550c9 Compare November 20, 2024 11:10
@ribaraka
Copy link

marshal.go Outdated
@@ -2208,7 +2210,7 @@ func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error {
return nil
}

return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: struct, []interface{}, array, slice.", info, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Unmarshaler is also accepted I think. Also the accepted types other than []interface{} have to be a pointer if I'm reading the code correctly (so *struct, *array, *slice)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Thank you.

marshal.go Show resolved Hide resolved
marshal.go Outdated
@@ -1706,7 +1708,7 @@ func unmarshalList(info TypeInfo, data []byte, value interface{}) error {
}
return nil
}
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
return unmarshalErrorf("can not unmarshal %s into %T. Accepted types: slice, array.", info, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it has to be *slice, *array

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

marshal.go Outdated
@@ -2392,7 +2394,7 @@ func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
}
k := rv.Elem()
if k.Kind() != reflect.Struct || !k.IsValid() {
return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
return unmarshalErrorf("cannot unmarshal %s into %T. Accepted types: Unmarshaler, UDTUnmarshaler, *map[string]interface{}, struct.", info, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

*struct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@joao-r-reis joao-r-reis changed the title Marshal error messages enhancement CASSGO-38 Marshal error messages enhancement Nov 22, 2024
@tengu-alt tengu-alt force-pushed the marshal-errors-enhancement branch from d6550c9 to 74254c8 Compare November 22, 2024 14:49
@jameshartig
Copy link
Contributor

It's not very common in Go for the error to explicitly provide the supported types but I can see how that's useful. Ideally a developer would be able to know the supported types before encountering an error. In #671 what made you think that timestamp would support an int? Was it just common sense or did you read something indicating as much?

I'd like to see improvements to the docs and a general improvement in the supported types. That said, nothing is wrong with this PR.

@tengu-alt
Copy link
Contributor Author

tengu-alt commented Jan 28, 2025

In #671 what made you think that timestamp would support an int? Was it just common sense or did you read something indicating as much?

I don't clearly understand your question. The driver already supports Timestamp marshal into int64, as said in the codebase and documentation.

@jameshartig
Copy link
Contributor

I don't clearly understand your question.

In #671 you ran into:

2016/03/08 08:35:36 can not marshal int into timestamp

Which meant you sent an int, that should be supported. I'm trying to understand how we can improve the documentation or API to make it more obvious which types are supported that doesn't involve someone having to parse an error after already writing code which assumed a certain type.

@tengu-alt
Copy link
Contributor Author

tengu-alt commented Jan 29, 2025

In #671 you ran into:

That was not me who ran into the error, the #671 was created by another person.

As Cassandra doc says: timestamp is encoded as int64. The default int in GO is a 32bit integer, so it is not supported.

IMHO the documentation in the marshal.go is pretty obvious, and it appears when you call the func Marshal().

@jameshartig
Copy link
Contributor

jameshartig commented Jan 29, 2025

That was not me who ran into the error, the #671 was created by another person.

Oh apologies for the confusion.

The default int in GO is a 32bit integer, so it is not supported.

Only on 32-bit systems, which are increasingly rare. We should be fine marshalling into Timestamp but maybe we just couldn't safely unmarshal

@tengu-alt tengu-alt force-pushed the marshal-errors-enhancement branch from 74254c8 to 87f1776 Compare January 31, 2025 10:46
Enhancement for marshal/unmarshal error messages.
Errors were updated by the supported types list.

patch by Oleksandr Luzhniy; reviewed by João Reis, James Hartig, Bohdan Siryk, for CASSGO-38
@tengu-alt tengu-alt force-pushed the marshal-errors-enhancement branch from 87f1776 to bc810b7 Compare January 31, 2025 10:49
@tengu-alt
Copy link
Contributor Author

@joao-r-reis I have updated the Changelog.md, should it be merged?

@joao-r-reis joao-r-reis merged commit 16366d4 into apache:trunk Jan 31, 2025
16 checks passed
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.

5 participants