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

use generics to reduce field boilerplate #6

Open
egonelbre opened this issue Sep 7, 2023 · 2 comments
Open

use generics to reduce field boilerplate #6

egonelbre opened this issue Sep 7, 2023 · 2 comments

Comments

@egonelbre
Copy link
Member

egonelbre commented Sep 7, 2023

There's a significant duplication of the field code:

type OauthToken_Scope_Field struct {
	_set   bool
	_null  bool
	_value string
}

func OauthToken_Scope(v string) OauthToken_Scope_Field {
	return OauthToken_Scope_Field{_set: true, _value: v}
}

func (f OauthToken_Scope_Field) value() interface{} {
	if !f._set || f._null {
		return nil
	}
	return f._value
}

func (OauthToken_Scope_Field) _Column() string { return "scope" }

Could be replaced by:

type OauthToken_Scope_Field struct { NullableField[string] }

func OauthToken_Scope(v string) OauthToken_Scope_Field {
	return OauthToken_Scope_Field{NullableField: FieldOf(v)}
}
func (OauthToken_Scope_Field) _Column() string { return "scope" }

It's not clear how big the benefit is here. Also, maybe there's a better approach for this.

@egonelbre egonelbre changed the title use generics to reduce boilerplate use generics to reduce field boilerplate Sep 7, 2023
@zeebo
Copy link
Collaborator

zeebo commented Sep 8, 2023

one of the things about the Field types is that the zero value is invalid (that's why there's both a _set and a _null), and you have to use the constructors to create values correctly. using embedding like this would negate that somewhat. it's also not very many lines of code saved.

@egonelbre
Copy link
Member Author

egonelbre commented Sep 8, 2023

Hmm, maybe there's a way to make something shorter? But, I was not able to come up with something shorter.

Maybe structs and delegating the field checks to lint time could work. i.e. get rid of the field types altogether.

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

No branches or pull requests

2 participants