ergo-services / ergo

An actor-based Framework with network transparency for creating event-driven architecture in Golang. Inspired by Erlang. Zero dependencies.
https://docs.ergo.services
MIT License
3.51k stars 138 forks source link

Encode unexported fields of go Structs #69

Closed heri16 closed 2 years ago

heri16 commented 3 years ago

Fixes #63

Changes:

Depends on #68

halturin commented 3 years ago

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()
    ...
}
halturin commented 3 years ago

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

halturin commented 2 years ago

not valid any more

heri16 commented 2 years ago

Nice one

heri16 commented 2 years ago

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.