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

Allow adding additional tags via goField directive #1575

Closed
asif-mahmud opened this issue Jul 25, 2021 · 2 comments
Closed

Allow adding additional tags via goField directive #1575

asif-mahmud opened this issue Jul 25, 2021 · 2 comments
Labels
enhancement New feature or request modelgen

Comments

@asif-mahmud
Copy link

It is not a bug report, more like a feature request. Currently, if we add additional tags in the generated model structures, and then re-generate after adding more types in the schema, the manually added tags get overwritten. So it requires to add those tags again after each generate. This could be made automatic by allowing the goField directive to have additional tag string or strings to include in the generated models. If i was not clear please let me know. Also if there is already some approach to take care of additional tags in the models, i ll be glad to check that out.

@lwc lwc added modelgen enhancement New feature or request labels Sep 17, 2021
@tprebs
Copy link
Contributor

tprebs commented Oct 8, 2021

Though this is not adding tags to goField, #1650 would resolve this issue as you could create a custom directive which adds the tags each time during generation.

e.g.

directive @goTags(tags: [String]!) on INPUT_FIELD_DEFINITION
    | FIELD_DEFINITION

and custom hook

func tagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *modelgen.Field) (*modelgen.Field, error) {

	c := fd.Directives.ForName("goTags")
	if c != nil {
		if ma := c.Arguments.ForName("tags"); ma != nil {
			if tags, err := ma.Value.Value(nil); err == nil {
				for _, tag := range tags.([]interface{}) {
					f.Tag += " "+tag.(string)
				}
			}
		}
	}

	return f, nil
}

usage

type SomeObject {
   field1 String @goTag(tags: ["validate:email","gorm:field_1"])
   field2 Int @goTag(tags: ["validate:gt="])
}

@tprebs
Copy link
Contributor

tprebs commented Oct 14, 2021

This issue can now be closed as there is a new directive extraTag for adding tags #1173

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request modelgen
Projects
None yet
Development

No branches or pull requests

4 participants