Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Introduce a FieldMapFromStruct() to avoid repetitions? #18

Closed
mark-kubacki opened this issue Aug 18, 2014 · 6 comments
Closed

Introduce a FieldMapFromStruct() to avoid repetitions? #18

mark-kubacki opened this issue Aug 18, 2014 · 6 comments

Comments

@mark-kubacki
Copy link
Contributor

What can we add to avoid repetitions such as this?
https://gist.github.com/wmark/73ee2abe30e13af93a18#file-mailgun-go-L3-L57

A FieldMapFromStruct() could use reflection to generate the FieldMap from tags(?). Because this has to be done once only, for example during the module's init() I believe it wouldn't count towards the claim Binding was »reflectionless«.

@bhcleek
Copy link
Collaborator

bhcleek commented Aug 20, 2014

@wmark I've taken a similar approach in sqldecoder. Though, I started with reflection based binding, and then added reflection-less. The advantage of such an approach, obviously, is that support "just works". But I do think it would be a strike against "reflectionless". Maybe the project would change to "reflection optional"...

@mholt
Copy link
Owner

mholt commented Aug 26, 2014

That's a big struct! I can understand wanting to eliminate the repetition of the field names. Though using tags could avoid that repetition, it introduces new repetition, namely the key (probably binding:"...").

Also, how would custom field binders be defined in field tags?

@mark-kubacki
Copy link
Contributor Author

If there were no binding:"..." that function would assume a default one, derived from the field's name.

The result is a FieldMap as usual. You could change fields after auto-generation.

If this is a long structure, then you certainly don't want to see another one I've stumbled upon in a project using a columnar store — it has 519 fields, and API calls to expose all. Easily manageable using Python.

I am currently looking into @bhcleek's sqldecoder and ffjson for inspirations to come up for a solution with a one-time cost.


BTW:
Go's map is currently only optimized for access by types uint32, uint64 and string if you ever wondered about performance.
Go 1.3.1: https://golang.org/src/pkg/runtime/hashmap.goc (search for KEYTYPE)
Go 1.4: https://code.google.com/p/go/source/browse/src/pkg/runtime/hashmap_fast.go

@mholt
Copy link
Owner

mholt commented Aug 26, 2014

Cool, good luck with that!

However I am, at this point, a little uncomfortable with introducing reflection into this package. Maybe there could be a codegen tool like JSON-to-Go that could help with generating a FieldMap for such large structs.

@bhcleek
Copy link
Collaborator

bhcleek commented Aug 26, 2014

+1 for code generation.
On Aug 26, 2014 7:15 AM, "Matt Holt" notifications@github.com wrote:

Cool, good luck with that!

However I am, at this point, a little uncomfortable with introducing
reflection into this package. Maybe there could be a codegen tool like
JSON-to-Go http://mholt.github.io/json-to-go/ that could help with
generating a FieldMap for such large structs.


Reply to this email directly or view it on GitHub
#18 (comment).

@bhcleek
Copy link
Collaborator

bhcleek commented Jan 13, 2015

Now that Go 1.4 is out, perhaps it would be better to provide a generator?

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

No branches or pull requests

3 participants