Skip to content

Commit

Permalink
created wrapper around GormValue
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed May 3, 2023
1 parent 919fb9c commit 4fd7266
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 43 deletions.
33 changes: 4 additions & 29 deletions models/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -394,27 +393,12 @@ func (p Properties) Find(name string) *Property {

// Scan scan value into Jsonb, implements sql.Scanner interface
func (p Properties) Value() (driver.Value, error) {
if len(p) == 0 {
return nil, nil
}
return p.AsJSON(), nil
return types.GenericStructValue(p, true)
}

// Scan scan value into Jsonb, implements sql.Scanner interface
func (p *Properties) Scan(val any) error {
if val == nil {
*p = make(Properties, 0)
return nil
}
var ba []byte
switch v := val.(type) {
case []byte:
ba = v
default:
return errors.New(fmt.Sprint("Failed to unmarshal properties value:", val))
}
err := json.Unmarshal(ba, p)
return err
return types.GenericStructScan(&p, val)
}

// GormDataType gorm common data type
Expand All @@ -423,20 +407,11 @@ func (Properties) GormDataType() string {
}

func (Properties) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case types.SqliteType:
return types.Text
case types.PostgresType:
return types.JSONBType
case types.SQLServerType:
return types.NVarcharType
}
return ""
return types.JSONGormDBDataType(db.Dialector.Name())
}

func (p Properties) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := json.Marshal(p)
return gorm.Expr("?", data)
return types.GormValue(p)
}

type ComponentRelationship struct {
Expand Down
10 changes: 3 additions & 7 deletions types/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"context"
"database/sql/driver"
"encoding/json"
"fmt"

"gorm.io/gorm"
Expand Down Expand Up @@ -120,8 +119,7 @@ func (Summary) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}

func (s Summary) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := json.Marshal(s)
return gorm.Expr("?", data)
return GormValue(s)
}

type ResourceSelectors []ResourceSelector
Expand Down Expand Up @@ -151,8 +149,7 @@ func (ResourceSelectors) GormDBDataType(db *gorm.DB, field *schema.Field) string
}

func (rs ResourceSelectors) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := json.Marshal(rs)
return gorm.Expr("?", string(data))
return GormValue(rs)
}

type ComponentCheck struct {
Expand Down Expand Up @@ -181,6 +178,5 @@ func (ComponentChecks) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}

func (cs ComponentChecks) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := json.Marshal(cs)
return gorm.Expr("?", string(data))
return GormValue(cs)
}
6 changes: 2 additions & 4 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"context"
"database/sql/driver"
"encoding/json"
"fmt"

"gorm.io/gorm"
Expand Down Expand Up @@ -35,7 +34,7 @@ func (c ConfigQuery) String() string {
}

if len(c.Tags) > 0 {
s += " " + fmt.Sprintf("%v", c.Tags)
s += " " + fmt.Sprintf("%+v", c.Tags)
}

return s
Expand All @@ -56,6 +55,5 @@ func (t ConfigQueries) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}

func (t ConfigQueries) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := json.Marshal(t)
return gorm.Expr("?", string(data))
return GormValue(t)
}
4 changes: 1 addition & 3 deletions types/log_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"context"
"database/sql/driver"
"encoding/json"

"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -32,6 +31,5 @@ func (t LogSelectors) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}

func (t LogSelectors) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := json.Marshal(t)
return gorm.Expr("?", string(data))
return GormValue(t)
}
5 changes: 5 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,8 @@ func JSONGormDBDataType(dialect string) string {

return ""
}

func GormValue(t any) clause.Expr {
data, _ := json.Marshal(t)
return gorm.Expr("?", string(data))
}

0 comments on commit 4fd7266

Please # to comment.