Skip to content

Commit

Permalink
Merge pull request #4 from wtask-go/feature/data-unmarshal
Browse files Browse the repository at this point in the history
Feature/data unmarshal
  • Loading branch information
wtask authored Apr 25, 2021
2 parents a0d5f42 + 7434396 commit 2c53536
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
5 changes: 2 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ linters-settings:
statements: 40

gocognit:
min-complexity: 20
min-complexity: 25

goconst:
min-len: 3
Expand Down Expand Up @@ -116,7 +116,7 @@ linters-settings:
skipRecvDeref: true

gocyclo:
min-complexity: 20
min-complexity: 25

gofmt:
simplify: true
Expand Down Expand Up @@ -240,7 +240,6 @@ linters:
- gocognit
- goconst
- gocritic
- goerr113
- gofmt
- golint
- goprintffuncname
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/wtask-go/mixpanel
go 1.16

require (
github.com/getkin/kin-openapi v0.53.0
github.com/getkin/kin-openapi v0.60.0
github.com/golangci/golangci-lint v1.39.0
github.com/psampaz/go-mod-outdated v0.7.0
github.com/psampaz/go-mod-outdated v0.8.0
github.com/santhosh-tekuri/jsonschema/v3 v3.0.1
)
67 changes: 67 additions & 0 deletions ingestion/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,70 @@ func (p *Properties) MarshalJSON() ([]byte, error) {

return json.Marshal(obj)
}

// UnmarshalJSON implements json.Unmarshaler interface.
// nolint:funlen // it is required parse raw data
func (p *Properties) UnmarshalJSON(raw []byte) error {
if p == nil {
return fmt.Errorf("unmarshal event.Properties to nil")
}

data := map[string]json.RawMessage{}
if err := json.Unmarshal(raw, &data); err != nil {
return err
}

unmarshal := func(key string, target interface{}) error {
if v, ok := data[key]; ok {
if err := json.Unmarshal(v, target); err != nil {
return err
}

delete(data, key)
}

return nil
}

if err := unmarshal("$insert_id", &p.InsertID); err != nil {
return err
}

if err := unmarshal("distinct_id", &p.DistinctID); err != nil {
return err
}

if err := unmarshal("ip", &p.IP); err != nil {
return err
}

var unix int64
if err := unmarshal("time", &unix); err != nil {
return err
}

if unix != 0 {
p.Time = time.Unix(unix, 0).UTC()
}

if err := unmarshal("token", &p.Token); err != nil {
return err
}

if len(data) > 0 {
if p.CustomProperties == nil {
p.CustomProperties = CustomProperties{}
}

for k, v := range data {
var c interface{}
if err := json.Unmarshal(v, &c); err != nil {
return err
}

p.CustomProperties[k] = c
}
}

return nil
}
43 changes: 43 additions & 0 deletions ingestion/event/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package event_test

import (
"encoding/json"
"reflect"
"testing"
"time"

"github.com/wtask-go/mixpanel/ingestion/event"
)

func TestData_json(t *testing.T) {
now := time.Now().Truncate(1 * time.Second).UTC()
src := event.Data{
Event: "test",
Properties: event.Properties{
InsertID: "test-insert-id",
DistinctID: "test-distinct-id",
IP: "ip-address",
Time: now,
Token: "test-token",
CustomProperties: event.CustomProperties{
"string": "test-custom-string",
"logical": true,
"number": 3.14,
},
},
}

srcJSON, err := json.Marshal(&src)
if err != nil {
t.Fatal(err)
}

data := event.Data{}
if err = json.Unmarshal(srcJSON, &data); err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(data, src) {
t.Fatalf("source: %+v, actual: %+v", src, data)
}
}

0 comments on commit 2c53536

Please sign in to comment.