-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc2f447
commit 8ec80ab
Showing
14 changed files
with
251 additions
and
7,683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package stream_chat //nolint: golint | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func copyMap(m map[string]interface{}) map[string]interface{} { | ||
m2 := make(map[string]interface{}, len(m)) | ||
for k, v := range m { | ||
m2[k] = v | ||
} | ||
return m2 | ||
} | ||
|
||
func removeFromMap(m map[string]interface{}, obj interface{}) { | ||
t := reflect.TypeOf(obj) | ||
for i := 0; i < t.NumField(); i++ { | ||
f := t.Field(i) | ||
if tag := f.Tag.Get("json"); tag != "" { | ||
tag = strings.Split(tag, ",")[0] | ||
delete(m, tag) | ||
} else { | ||
delete(m, f.Name) | ||
} | ||
} | ||
} | ||
|
||
func addToMapAndMarshal(m map[string]interface{}, obj interface{}) ([]byte, error) { | ||
m2 := copyMap(m) | ||
|
||
data, err := json.Marshal(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := json.Unmarshal(data, &m2); err != nil { | ||
return nil, err | ||
} | ||
return json.Marshal(m2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package stream_chat // nolint: golint | ||
|
||
import ( | ||
"encoding/json" | ||
"math/rand" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func randomExtraData(in interface{}) { | ||
v := reflect.ValueOf(in).Elem() | ||
if v.Kind() != reflect.Struct { | ||
return | ||
} | ||
f := v.FieldByName("ExtraData") | ||
f.Set(reflect.ValueOf(map[string]interface{}{ | ||
"extra_data": map[string]interface{}{ | ||
"mystring": randomString(10), | ||
"mybool": rand.Float64() < 0.5, | ||
}, | ||
"data": "custom", | ||
"custom_data": "really_custom", | ||
"extra": map[string]interface{}{ | ||
randomString(10): randomString(10), | ||
}, | ||
"stream": randomString(10), | ||
"my_score": float64(rand.Intn(100)), | ||
})) | ||
} | ||
|
||
func testInvariantJSON(t *testing.T, in, in2 interface{}) { | ||
// put random | ||
randomExtraData(in) | ||
|
||
// marshal given | ||
data, err := json.Marshal(in) | ||
require.NoError(t, err) | ||
|
||
// unmarshal again | ||
require.NoError(t, json.Unmarshal(data, in2)) | ||
|
||
// ensure they are same | ||
require.Equal(t, in, in2) | ||
} | ||
|
||
func TestJSON(t *testing.T) { | ||
var c, c2 Channel | ||
testInvariantJSON(t, &c, &c2) | ||
|
||
var u, u2 User | ||
testInvariantJSON(t, &u, &u2) | ||
|
||
var e, e2 Event | ||
testInvariantJSON(t, &e, &e2) | ||
|
||
var m, m2 Message | ||
testInvariantJSON(t, &m, &m2) | ||
|
||
var mr, mr2 messageRequestMessage | ||
testInvariantJSON(t, &mr, &mr2) | ||
|
||
var a, a2 Attachment | ||
testInvariantJSON(t, &a, &a2) | ||
|
||
var r, r2 Reaction | ||
testInvariantJSON(t, &r, &r2) | ||
} |
Oops, something went wrong.