Skip to content

Commit e3ef90e

Browse files
chore: test jsonb
1 parent 42db751 commit e3ef90e

File tree

4 files changed

+78
-44
lines changed

4 files changed

+78
-44
lines changed

dao/dbmodel/feature_flag.go

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
package dbmodel
22

33
import (
4-
"database/sql/driver"
5-
"encoding/json"
64
"time"
75

86
"github.com/go-feature-flag/app-api/model"
97
"github.com/google/uuid"
108
)
119

12-
type JSONB json.RawMessage
13-
14-
func (j JSONB) Value() (driver.Value, error) {
15-
valueString, err := json.Marshal(j)
16-
return string(valueString), err
17-
}
18-
19-
func (j *JSONB) Scan(value interface{}) error {
20-
return json.Unmarshal(value.([]byte), &j)
21-
}
22-
2310
type FeatureFlag struct {
2411
ID uuid.UUID `db:"id"`
2512
Name string `db:"name"`
@@ -41,36 +28,26 @@ func FromModelFeatureFlag(mff model.FeatureFlag) (FeatureFlag, error) {
4128
if err != nil {
4229
return FeatureFlag{}, err
4330
}
44-
45-
variations, err := json.Marshal(mff.Variations)
46-
if err != nil {
47-
return FeatureFlag{}, err
48-
}
49-
50-
var metadata JSONB
51-
if mff.Metadata != nil {
52-
metadataBytes, err := json.Marshal(mff.Metadata)
53-
if err != nil {
54-
return FeatureFlag{}, err
55-
}
56-
metadata = JSONB(metadataBytes)
57-
}
58-
59-
return FeatureFlag{
31+
ff := FeatureFlag{
6032
ID: id,
6133
Name: mff.Name,
6234
Description: mff.Description,
63-
Variations: JSONB(variations),
6435
Type: mff.VariationType,
6536
BucketingKey: mff.BucketingKey,
66-
Metadata: metadata,
6737
TrackEvents: mff.TrackEvents,
6838
Disable: mff.Disable,
6939
Version: mff.Version,
7040
CreatedDate: mff.CreatedDate,
7141
LastUpdatedDate: mff.LastUpdatedDate,
7242
LastModifiedBy: mff.LastModifiedBy,
73-
}, nil
43+
}
44+
if mff.Variations != nil {
45+
ff.Variations = JSONB(*mff.Variations)
46+
}
47+
if mff.Metadata != nil {
48+
ff.Metadata = JSONB(*mff.Metadata)
49+
}
50+
return ff, nil
7451
}
7552

7653
func (ff *FeatureFlag) ToModelFeatureFlag(rules []Rule) (model.FeatureFlag, error) {
@@ -87,19 +64,14 @@ func (ff *FeatureFlag) ToModelFeatureFlag(rules []Rule) (model.FeatureFlag, erro
8764
}
8865
apiRules = append(apiRules, convertedRule)
8966
}
90-
91-
var variations map[string]*interface{}
92-
err := json.Unmarshal(ff.Variations, &variations)
93-
if err != nil {
94-
return model.FeatureFlag{}, err
67+
variations := make(map[string]interface{})
68+
if ff.Variations != nil {
69+
variations = ff.Variations
9570
}
96-
97-
var metadata map[string]interface{}
98-
err = json.Unmarshal(ff.Metadata, &metadata)
99-
if err != nil {
100-
return model.FeatureFlag{}, err
71+
metadata := make(map[string]interface{})
72+
if ff.Metadata != nil {
73+
metadata = ff.Metadata
10174
}
102-
10375
return model.FeatureFlag{
10476
ID: ff.ID.String(),
10577
Name: ff.Name,

dao/dbmodel/jsonb.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dbmodel
2+
3+
import (
4+
"database/sql/driver"
5+
"encoding/json"
6+
"errors"
7+
)
8+
9+
type JSONB map[string]interface{}
10+
11+
func (a JSONB) Value() (driver.Value, error) {
12+
return json.Marshal(a)
13+
}
14+
15+
func (a *JSONB) Scan(value interface{}) error {
16+
b, ok := value.([]byte)
17+
if !ok {
18+
return errors.New("type assertion to []byte failed")
19+
}
20+
21+
return json.Unmarshal(b, &a)
22+
}

dao/dbmodel/jsonb_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package dbmodel_test
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/go-feature-flag/app-api/dao/dbmodel"
8+
_ "github.com/lib/pq"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
var theTestMap = map[string]interface{}{
13+
"key": "value",
14+
"another_key": "another_value",
15+
"nested_key": map[string]interface{}{
16+
"child_key": "child_value",
17+
},
18+
}
19+
20+
var theTestString = `{"field":{"another_key":"another_value","key":"value","nested_key":{"child_key":"child_value"}}}`
21+
22+
type TestStruct struct {
23+
Field dbmodel.JSONB `json:"field"`
24+
}
25+
26+
func TestJSONBMarshalling(t *testing.T) {
27+
theTest := TestStruct{
28+
Field: theTestMap,
29+
}
30+
b, err := json.Marshal(theTest)
31+
assert.Nil(t, err)
32+
assert.Equal(t, theTestString, string(b))
33+
}
34+
35+
func TestJSONBUnmarshalling(t *testing.T) {
36+
theTest := TestStruct{}
37+
err := json.Unmarshal([]byte(theTestString), &theTest)
38+
assert.Nil(t, err)
39+
assert.Equal(t, dbmodel.JSONB(theTestMap), theTest.Field)
40+
}

model/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type FeatureFlag struct {
2424
VariationType FlagType `json:"type" db:"type"`
2525
// Variations are all the variations available for this flag. The minimum is 2 variations and, we don't have any max
2626
// limit except if the variationValue is a bool, the max is 2.
27-
Variations *map[string]*interface{} `json:"variations,omitempty"`
27+
Variations *map[string]interface{} `json:"variations,omitempty"`
2828

2929
// Rules is the list of Rule for this flag.
3030
// This an optional field.

0 commit comments

Comments
 (0)