Skip to content

Commit

Permalink
add fiber decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
putuadityabayu committed Feb 10, 2024
1 parent 49de68f commit d2e898c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions string_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
pg "github.com/lib/pq"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"reflect"
)

type StringArray struct {
Expand Down Expand Up @@ -100,6 +101,17 @@ func (StringArray) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return ""
}

func (StringArray) FiberConverter(value string) reflect.Value {
var tmp pg.StringArray
s := StringArray{true, false, pg.StringArray{}}

if err := json.Unmarshal([]byte(value), &tmp); err != nil {
s = NewStringArray(tmp, true, true)
}

return reflect.ValueOf(s)
}

//func (d StringArray) GormValue(_ context.Context, db *gorm.DB) clause.Expr {
// data, _ := d.MarshalJSON()
// switch db.Dialector.Name() {
Expand Down
12 changes: 12 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"

"gorm.io/datatypes"
"gorm.io/gorm"
Expand Down Expand Up @@ -114,6 +115,17 @@ func (Type[D]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return ""
}

func (Type[D]) FiberConverter(value string) reflect.Value {
var tmp D
s := Type[D]{true, false, tmp}

if err := json.Unmarshal([]byte(value), &tmp); err != nil {
s = NewType(tmp, true, true)
}

return reflect.ValueOf(s)
}

//func (js Type[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
// if !js.Valid {
// return clause.Expr{SQL: "?", Vars: []any{"NULL"}, WithoutParentheses: true}
Expand Down

0 comments on commit d2e898c

Please sign in to comment.