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

Reflection-based marshaling / unmarshaling #149

Merged
merged 13 commits into from
Mar 29, 2017

Conversation

tro3
Copy link

@tro3 tro3 commented Mar 21, 2017

Going after #146, @pelletier this initial commit is just to give you a feel for the direction I am heading with this. Still need to add Map and Array handling, error handling (with position info for unmarshaling), and lots of tests. Please let me know your thoughts, so I can shift course early if needed.

One question - do you want to follow the encoding/json pattern of ignoring non-exported fields?

@pelletier
Copy link
Owner

Good job! That's pretty much what I had in mind. I'd like to stay as close as possible to the behavior of encoding/json. So yes, I would ignore non-exported fields.

@tro3 tro3 changed the title [WIP]Reflection-based marshaling / unmarshaling Reflection-based marshaling / unmarshaling Mar 25, 2017
@tro3
Copy link
Author

tro3 commented Mar 25, 2017

@pelletier - I've removed the WIP tag. This is ready for review. The only feature it does not have is the handling of interface{} types in the object being marshalled. That is a big enough job (I think) to warrant another PR.

@carolynvs, @sdboyer - Would love to hear feedback on how usable this appears in the dep case.

@tro3
Copy link
Author

tro3 commented Mar 25, 2017

Hmm, it occurs I still need documentation... Will add.

@carolynvs
Copy link
Contributor

@tro3 I think this is pretty close! 🎉 I would just need omitempty and could replace the manual mapping in my PR.

@tro3
Copy link
Author

tro3 commented Mar 29, 2017

I'm on it.

@tro3
Copy link
Author

tro3 commented Mar 29, 2017

@carolynvs - omitempty is functional. @pelletier - ready for review. Note to both that I have made pointers automatically omitempty, as TOML wants null values dropped from the file, per toml-lang/toml#30

Copy link
Owner

@pelletier pelletier left a comment

Choose a reason for hiding this comment

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

Great work overall! I'd like to see the test coverage increased for that module though. I'm seeing code paths that would deserve some testing, especially concerning pointers handling.

marshal.go Outdated
val := tval.Get(key)
mvalf, err := valueFromToml(mtypef.Type, val)
if err != nil {
if err.Error()[0] == '(' {
Copy link
Owner

Choose a reason for hiding this comment

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

Seems weird how to do some logic based off the first character of the error message. Do you mind elaborating on how this works?

Copy link
Author

Choose a reason for hiding this comment

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

This is just to prevent every level of hierarchy adding its own location info. I'll take a look to see if there is a cleaner error handling method.

Copy link
Owner

Choose a reason for hiding this comment

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

Got it. If you don't find a cleaner way to do it, I think just adding a comment in places you do that is good enough for now. There is the broader case of how we report errors in toml that needs to be addressed too.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed. I've already moved to a separate function formatError with a comment describing the "why". Easier to rework later, if needed. Just fighting test coverage now - forcing omitempty onto pointers has resulted in some unreachable(/untestable) code.

marshal.go Outdated
func Marshal(v interface{}) ([]byte, error) {
mtype := reflect.TypeOf(v)
if mtype.Kind() != reflect.Struct {
return []byte{}, errors.New("Only a Struct can be marshaled to TOML")
Copy link
Owner

Choose a reason for hiding this comment

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

struct, lower s :)

Copy link
Author

Choose a reason for hiding this comment

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

will fix

@tro3
Copy link
Author

tro3 commented Mar 29, 2017

Will work on the above and add test coverage

@tro3
Copy link
Author

tro3 commented Mar 29, 2017

@pelletier - Incorporated the feedback and did indeed find some holes due to lack of test coverage. Let me know what more is needed.

Copy link
Owner

@pelletier pelletier left a comment

Choose a reason for hiding this comment

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

Awesome!

@pelletier pelletier merged commit e32a2e0 into pelletier:master Mar 29, 2017
@tro3 tro3 deleted the reflection_marshal branch March 29, 2017 21:50
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.

3 participants