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

Add possibility to hook into modelgen plugin #897

Merged
merged 3 commits into from
Nov 11, 2019
Merged

Add possibility to hook into modelgen plugin #897

merged 3 commits into from
Nov 11, 2019

Conversation

mskrip
Copy link
Contributor

@mskrip mskrip commented Oct 11, 2019

I realize there already is a branch modelgen-structtags for the mentioned issue and for what this change tries to accomplish. I'm opening a PR nonetheless at least as a suggestion since the feature has more general implications (possibility to mutate the whole generated models before rendering). Feel free to close this if the mentioned branch is close to solving the issue.


This change introduces option to implement custom hook for model
generation plugin without the need to completly copy the whole modelgen plugin.

One very possible case is as described in #876 and with this change the solution for
that can be:

func mutateHook(b *ModelBuild) *ModelBuild {
	for _, model := range b.Models {
		for _, field := range model.Fields {
			field.Tag += ` orm_binding:"` + model.Name + `.`  +  field.Name + `"`
		}
	}

	return b
}

...

func main() {
    p := modelgen.Plugin {
        MutateHook: mutateHook,
    }

    ...
}

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

@coveralls
Copy link

coveralls commented Oct 11, 2019

Coverage Status

Coverage decreased (-0.04%) to 62.027% when pulling 2a269dd on mskrip:modelgen-hook into 99a55da on 99designs:master.

@JulienBreux
Copy link

Good job!

@unlinking
Copy link
Contributor

Very useful feature for bson, mapstructure, camel to under score...

@vektah
Copy link
Collaborator

vektah commented Nov 11, 2019

This needs some docs before we can :shipit:

This change introduces option to implement custom hook for model
generation plugin without the need to completly copy the whole `modelgen` plugin.

One very possible case is as described in #876 and with this change the solution for
that can be:

```golang
func mutateHook(b *ModelBuild) *ModelBuild {
	for _, model := range b.Models {
		for _, field := range model.Fields {
			field.Tag += ` orm_binding:"` + model.Name + `.`  +  field.Name + `"`
		}
	}

	return b
}

...

func main() {
    p := modelgen.Plugin {
        MutateHook: mutateHook,
    }

    ...
}

```
@vektah
Copy link
Collaborator

vektah commented Nov 11, 2019

Those docs look great, thanks!

@vektah vektah merged commit 9cfd817 into 99designs:master Nov 11, 2019
cgxxv pushed a commit to cgxxv/gqlgen that referenced this pull request Mar 25, 2022
Add possibility to hook into modelgen plugin
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 this pull request may close these issues.

None yet

5 participants