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

Encode unexported fields of go Structs #69

Closed
wants to merge 4 commits into from

Conversation

heri16
Copy link

@heri16 heri16 commented Aug 2, 2021

Fixes #63

Changes:

  • Encode unexported fields of goStructs.
  • Use encoding.TextMarshaler on unaddressable goStructs (e.g. golang stdlib time.Now())

Depends on #68

@halturin
Copy link
Collaborator

halturin commented Aug 2, 2021

Instead of trying to encode "unexported" fields, I would add checking for the method Marshal() in the encoder and Unmarshal() for the TermIntoStruct/TermMapIntoStruct accordingly. I believe, "unexported" field must be hidden from the outside world. That's why they called it "unexported" isn't it? :)

var bin []byte
var err error
if obj, ok := term.(interface{Marshal()}); ok {
    bin, err = obj.Marshal()
    ...
}

encode/decode strings longer than 65535
Use encoding.TextMarshaler on unaddressable goStructs.
@halturin
Copy link
Collaborator

There is Marshal/Unmarshal feature in the coming release. So this PR won't be accepted.

@halturin
Copy link
Collaborator

not valid any more

@halturin halturin closed this Oct 11, 2021
@heri16
Copy link
Author

heri16 commented Oct 12, 2021

Nice one

@heri16
Copy link
Author

heri16 commented Oct 12, 2021

Instead of trying to encode "unexported" fields, I would add checking for the method Marshal() in the encoder and Unmarshal() for the TermIntoStruct/TermMapIntoStruct accordingly. I believe, "unexported" field must be hidden from the outside world. That's why they called it "unexported" isn't it? :)

var bin []byte
var err error
if obj, ok := term.(interface{Marshal()}); ok {
    bin, err = obj.Marshal()
    ...
}

That may be true most of the time, but not all the time. Third-party libraries commonly have structs that have unexported fields that cannot be accessed unless you can modify the third-party source code to add accessor methods.

And unmarshall / marshall methods that you add would not be able to read those same unexported fields.

If the goal is transparent and efficient transport of in-memory state across the wire, then the use case could be valid.

Documenting how this could be done via the new ergo API would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panic when sending time.Time
2 participants