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

Support unexported types #24

Closed
GeertJohan opened this issue Nov 19, 2014 · 6 comments · Fixed by #25
Closed

Support unexported types #24

GeertJohan opened this issue Nov 19, 2014 · 6 comments · Fixed by #25

Comments

@GeertJohan
Copy link

I have a package with several types that must be unexported, but are marshalled/unmarshalled to/from json. Running ffjson returns several errors:

> ffjson server.gen.go
Error: Go Run Failed for: /tmp/ffjson-inception430580992.go
STDOUT:

STDERR:
# command-line-arguments
/tmp/ffjson-inception430580992.go:19: cannot refer to unexported name chatservice.angoInMsg
/tmp/ffjson-inception430580992.go:21: cannot refer to unexported name chatservice.angoClientArgsDataDisplayNotification
/tmp/ffjson-inception430580992.go:23: cannot refer to unexported name chatservice.angoServerArgsDataAdd8
/tmp/ffjson-inception430580992.go:25: cannot refer to unexported name chatservice.angoServerRetsDataDostuff
/tmp/ffjson-inception430580992.go:27: cannot refer to unexported name chatservice.angoOutMsg
/tmp/ffjson-inception430580992.go:29: cannot refer to unexported name chatservice.angoClientRetsDataAskQuestion
/tmp/ffjson-inception430580992.go:31: cannot refer to unexported name chatservice.angoServerArgsDataDostuff
/tmp/ffjson-inception430580992.go:33: cannot refer to unexported name chatservice.angoOutError
/tmp/ffjson-inception430580992.go:37: cannot refer to unexported name chatservice.angoServerArgsDataAdd
/tmp/ffjson-inception430580992.go:39: cannot refer to unexported name chatservice.angoServerRetsDataAdd8
/tmp/ffjson-inception430580992.go:39: too many errors

:

I believe that creating ffjson with unexported types should be possible.

@pquerna
Copy link
Owner

pquerna commented Nov 21, 2014

Can you give me a small example struct that does this?

@pquerna
Copy link
Owner

pquerna commented Nov 22, 2014

eg, under encoding/json, unexported types are not Marshalled, even if they have JSON tags:

http://play.golang.org/p/O8bS4JRfO0

@GeertJohan
Copy link
Author

@GeertJohan
Copy link
Author

The play example shows how an unexported type can still be marshalled itself.. Fields in the struct type must be exported ofcourse.

The situation I have is a bit different than the play example. In my case I have packages (not main/command) that have unexported types which are being marshalled to json by that package itself. The smallest example I can give of this is:

package mypkg

import "os"
import "encoding/json"

type unexported struct {
    Exported string
}

func DoStuff() {
    u := &unexported{
        Exported: "foobar",
    }
    json.NewEncoder(os.Stdout).Encode(u)
}

Running ffjson on that file exits with:

Error: Go Run Failed for: /tmp/ffjson-inception660098357.go
STDOUT:

STDERR:
# command-line-arguments
/tmp/ffjson-inception660098357.go:15: cannot refer to unexported name mypkg.unexported
/tmp/ffjson-inception660098357.go:15: undefined: mypkg.unexported

@pquerna
Copy link
Owner

pquerna commented Nov 22, 2014

Ok, cool, this should be easy to fix. Will take a look shortly.

@GeertJohan
Copy link
Author

Thanks!

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 a pull request may close this issue.

2 participants