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 {