[Enhancement] Support struct tags in schema #15

Open
knadh opened this Issue Jan 5, 2017 · 1 comment

Projects

None yet

1 participant

@knadh
knadh commented Jan 5, 2017

Firstly, thanks for the library!

In a reasonably large codebase, structs generated by gencode from schema may be shared by various parts. A simple example would first populating a struct with a json.Unmarshal and then marshalling with struct.EncodeMsg. Here, the first step may require json tags.

As things stand, there are two approaches, both highly impractical.

  1. Generate struct using gencode and manually edit the generated struct and add tags.
  2. In addition to the schema, define Go structs in the codebase with tags that match the schema and copy values between these and gencode's generated structs.

Proposed solution
Add tag support to schema definitions, where they are simply copied over to the generated structs. They needn't change any existing logic in gencode, nor do they have to affect gencode's encoding or decoding logic.

eg:

struct Person {
	Name    string   `json:"name" redis:"name"`
	Age     int
}

Generated struct in x.gen.go

struct Person type {
	Name    string   `json:"name" redis:"name"`
	Age     int
}

I can see that this is possible by tweaking the grammar in parser.go, but I'm not really able to figure out how to do it with andyleap/parser as it has no documentation.

Thank you.

@knadh
knadh commented Jan 5, 2017

Nevermind. Figured out how to make andyleap/parser work and created a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment