From 8ad43fc997166a4dae101e4ab1fac9c30e818b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omer=20Murat=20Y=C4=B1ld=C4=B1r=C4=B1m?= Date: Thu, 15 Oct 2020 15:54:01 +0300 Subject: [PATCH] Add Json field support (#3) * use go modules * do not use dep for travis * Use github.com/putdotio/pas as module name * run inside docker containers * move go-mod files above to prevent download modules for each run * Add Json field support --- config.toml | 4 ++++ internal/property/json.go | 28 ++++++++++++++++++++++++++++ internal/property/type.go | 1 + 3 files changed, 33 insertions(+) create mode 100644 internal/property/json.go diff --git a/config.toml b/config.toml index ccca911..e6fc54b 100644 --- a/config.toml +++ b/config.toml @@ -10,3 +10,7 @@ email = "string" [Events.page_viewed] path = "string" + +[Events.event_foo] +name = "string" +data = "json" diff --git a/internal/property/json.go b/internal/property/json.go new file mode 100644 index 0000000..cd75d07 --- /dev/null +++ b/internal/property/json.go @@ -0,0 +1,28 @@ +package property + +import ( + "database/sql/driver" + "encoding/json" + "fmt" +) + +type Json struct{} + +func newJson(args []string) (typeInterface, error) { + if len(args) > 0 { + return nil, fmt.Errorf("invalid arguments: %q", args) + } + return Json{}, nil +} + +func (j Json) ColumnType() string { + return "json" +} + +func (j Json) ConvertValue(v interface{}) (driver.Value, error) { + b, err := json.Marshal(v) + if err != nil { + return nil, fmt.Errorf("value is not json: %v", v) + } + return b, nil +} diff --git a/internal/property/type.go b/internal/property/type.go index 2ed7fa6..26eda18 100644 --- a/internal/property/type.go +++ b/internal/property/type.go @@ -38,6 +38,7 @@ var types = map[string]func([]string) (typeInterface, error){ "date": newDate, "datetime": newDateTime, "decimal": newDecimal, + "json": newJson, } func (t2 *Type) UnmarshalText(text []byte) error {