dbx icon indicating copy to clipboard operation
dbx copied to clipboard

use generics to reduce field boilerplate

Open egonelbre opened this issue 2 years ago • 2 comments

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 avatar Sep 07 '23 14:09 egonelbre

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.

zeebo avatar Sep 08 '23 14:09 zeebo

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.

egonelbre avatar Sep 08 '23 14:09 egonelbre