-
Notifications
You must be signed in to change notification settings - Fork 125
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
Postgres arrays returned as string #53
Comments
Jet currently doesn't have support for arrays. type Session struct {
model.Sessions
}
func (s Session) GetScopes() []string {
return ... convert s.Scopes to []string
}
func (s* Session) SetScopes(scopes []string) {
s.Scopes = ... convert []string into postgres array representation.
} |
Thanks, here's my sketchy workaround for pgx: package pg
import (
"github.com/jackc/pgtype"
)
func DecodeTextArray(s string) ([]string, error) {
ta := &pgtype.TextArray{}
if err := ta.Scan(s); err != nil {
return nil, err
}
arr := make([]string, len(ta.Elements))
for i, text := range ta.Elements {
arr[i] = text.String
}
return arr, nil
}
func EncodeTextArray(arr []string) (string, error) {
elements := make([]pgtype.Text, len(arr))
for i, s := range arr {
elements[i] = pgtype.Text{
Status: pgtype.Present,
String: s,
}
}
v, err := (&pgtype.TextArray{
Elements: elements,
Dimensions: []pgtype.ArrayDimension{
{Length: int32(len(arr)), LowerBound: 1},
},
Status: pgtype.Present,
}).Value()
if err != nil {
return "", err
}
if v == nil {
return "{}", nil
}
return v.(string), nil
} |
Ability to change field type of generated model type is now added to type StringArray []string
func (s StringArray) Scan(value interface{}) error {
...
}
func (s StringArray) Value() (driver.Value, error) {
...
} |
Ability to change field type of generated model type is included in v2.6.0 release. |
Hi @go-jet can you please clarify , how this could be used with cli generator? |
It can't be used from cli generator, only from Generator customization. |
thanks for the clarification! :( |
For anyone looking for implementation, here what works for me: import (
"database/sql/driver"
"strings"
)
type StringArray []string
func (s *StringArray) Scan(value interface{}) error {
input := strings.Trim(value.(string), "{}")
*s = StringArray(strings.Split(input, ","))
return nil
}
func (s StringArray) Value() (driver.Value, error) {
return "{" + strings.Join(s, ",") + "}", nil
} And in your custom generator you should add: func useArray(f *template.TableModelField, t metadata.Table, c metadata.Column) {
if c.DataType.Kind == metadata.ArrayType {
if c.DataType.Name == "text" {
// Note that StringArray should be in separate package
f.Type = template.NewType(typings.StringArray{})
}
}
} And a few lines of code later: ...
UseModel(template.DefaultModel().
UseTable(func(t metadata.Table) template.TableModel {
return template.DefaultTableModel(t).
UseField(func(c metadata.Column) template.TableModelField {
defaultTableModelField := template.DefaultTableModelField(c)
useArray(&defaultTableModelField, t, c)
return defaultTableModelField
})
})
... |
instead of defining a new type it is possible to use directly |
I have this table:
However, it generates this code:
scopes
has been emitted asstring
rather than[]string
, and results in the funny Postgres{foo, bar}
string. Is it possible to scan directly into an array?The text was updated successfully, but these errors were encountered: