storj / dbx

A neat codegen-based database wrapper written in Go
Apache License 2.0
24 stars 3 forks source link

use generics to reduce field boilerplate #6

Open egonelbre opened 1 year ago

egonelbre commented 1 year ago

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.

zeebo commented 1 year ago

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 commented 1 year ago

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.