From 6142371a87905a17c01220ca9f4405978829ff53 Mon Sep 17 00:00:00 2001 From: Peter Grlica Date: Mon, 11 Oct 2021 16:25:00 +0200 Subject: [PATCH] Added Bytes type, fixed locale regen --- compose/service/locale.gen.go | 2 - .../templates/gocode/locale/service.go.tpl | 2 - pkg/expr/expr_types.gen.go | 49 +++++++++++++++++++ pkg/expr/expr_types.go | 11 +++++ pkg/expr/expr_types.yaml | 5 ++ store/tests/gen_test.go | 3 +- system/automation/expr_types.gen.go | 19 ++++--- system/automation/expr_types.yaml | 2 +- 8 files changed, 76 insertions(+), 17 deletions(-) diff --git a/compose/service/locale.gen.go b/compose/service/locale.gen.go index a2eeecef3..7e70de770 100644 --- a/compose/service/locale.gen.go +++ b/compose/service/locale.gen.go @@ -13,9 +13,7 @@ package service import ( "context" - "github.com/cortezaproject/corteza-server/compose/types" - "github.com/cortezaproject/corteza-server/pkg/actionlog" intAuth "github.com/cortezaproject/corteza-server/pkg/auth" "github.com/cortezaproject/corteza-server/pkg/errors" diff --git a/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl b/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl index b17e4b43f..03d892764 100644 --- a/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl +++ b/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl @@ -9,8 +9,6 @@ import ( {{- range .Imports }} {{ . }} {{- end }} - - "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/actionlog" intAuth "github.com/cortezaproject/corteza-server/pkg/auth" "github.com/cortezaproject/corteza-server/pkg/errors" diff --git a/pkg/expr/expr_types.gen.go b/pkg/expr/expr_types.gen.go index f64992d9a..64b8df24d 100644 --- a/pkg/expr/expr_types.gen.go +++ b/pkg/expr/expr_types.gen.go @@ -171,6 +171,55 @@ func (t Boolean) Compare(to TypedValue) (int, error) { return compareToBoolean(t, to) } +// Bytes is an expression type, wrapper for []byte type +type Bytes struct { + value []byte + mux sync.RWMutex +} + +// NewBytes creates new instance of Bytes expression type +func NewBytes(val interface{}) (*Bytes, error) { + if c, err := CastToBytes(val); err != nil { + return nil, fmt.Errorf("unable to create Bytes: %w", err) + } else { + return &Bytes{value: c}, nil + } +} + +// Get return underlying value on Bytes +func (t *Bytes) Get() interface{} { + t.mux.RLock() + defer t.mux.RUnlock() + return t.value +} + +// GetValue returns underlying value on Bytes +func (t *Bytes) GetValue() []byte { + t.mux.RLock() + defer t.mux.RUnlock() + return t.value +} + +// Type return type name +func (Bytes) Type() string { return "Bytes" } + +// Cast converts value to []byte +func (Bytes) Cast(val interface{}) (TypedValue, error) { + return NewBytes(val) +} + +// Assign new value to Bytes +// +// value is first passed through CastToBytes +func (t *Bytes) Assign(val interface{}) error { + if c, err := CastToBytes(val); err != nil { + return err + } else { + t.value = c + return nil + } +} + // DateTime is an expression type, wrapper for *time.Time type type DateTime struct { value *time.Time diff --git a/pkg/expr/expr_types.go b/pkg/expr/expr_types.go index c4cbc3e84..670fccd3e 100644 --- a/pkg/expr/expr_types.go +++ b/pkg/expr/expr_types.go @@ -371,6 +371,17 @@ func CastToString(val interface{}) (out string, err error) { } } +func CastToBytes(val interface{}) (out []byte, err error) { + s, err := CastToString(val) + + if err != nil { + return + } + + out = []byte(s) + return +} + func CastToStringSlice(val interface{}) (out []string, err error) { return cast.ToStringSliceE(UntypedValue(val)) } diff --git a/pkg/expr/expr_types.yaml b/pkg/expr/expr_types.yaml index f7657b11b..f5d6cbfa8 100644 --- a/pkg/expr/expr_types.yaml +++ b/pkg/expr/expr_types.yaml @@ -44,6 +44,11 @@ types: default: '""' comparable: true + Bytes: + as: '[]byte' + default: 'nil' + comparable: false + Handle: as: 'string' default: '""' diff --git a/store/tests/gen_test.go b/store/tests/gen_test.go index a8d44cb38..411f8e75c 100644 --- a/store/tests/gen_test.go +++ b/store/tests/gen_test.go @@ -48,9 +48,8 @@ package tests // import ( - "testing" - "github.com/cortezaproject/corteza-server/store" + "testing" ) func testAllGenerated(t *testing.T, s store.Storer) { diff --git a/system/automation/expr_types.gen.go b/system/automation/expr_types.gen.go index dfd49f62e..272088bdc 100644 --- a/system/automation/expr_types.gen.go +++ b/system/automation/expr_types.gen.go @@ -11,11 +11,10 @@ package automation import ( "context" "fmt" - "sync" - . "github.com/cortezaproject/corteza-server/pkg/expr" "github.com/cortezaproject/corteza-server/pkg/rbac" "github.com/cortezaproject/corteza-server/system/types" + "sync" ) var _ = context.Background @@ -181,7 +180,7 @@ func queueMessageTypedValueSelector(res *types.QueueMessage, k string) (TypedVal case "Queue": return NewString(res.Queue) case "Payload": - return NewString(res.Payload) + return NewBytes(res.Payload) } return nil, fmt.Errorf("unknown field '%s'", k) @@ -198,14 +197,14 @@ func assignToQueueMessage(res *types.QueueMessage, k string, val interface{}) er res.Queue = aux return nil - // case "Payload": - // aux, err := CastToString(val) - // if err != nil { - // return err - // } + case "Payload": + aux, err := CastToBytes(val) + if err != nil { + return err + } - // res.Payload = aux - // return nil + res.Payload = aux + return nil } return fmt.Errorf("unknown field '%s'", k) diff --git a/system/automation/expr_types.yaml b/system/automation/expr_types.yaml index 11e52c873..032716354 100644 --- a/system/automation/expr_types.yaml +++ b/system/automation/expr_types.yaml @@ -69,7 +69,7 @@ types: as: '*types.QueueMessage' struct: - { name: 'Queue', exprType: 'String', goType: 'string' } - - { name: 'Payload', exprType: 'String', goType: '[]byte' } + - { name: 'Payload', exprType: 'Bytes', goType: '[]byte' } RbacResource: as: 'rbac.Resource'