Skip to content

Commit

Permalink
Merge pull request #1223 from nyaruka/more_deprecated
Browse files Browse the repository at this point in the history
Deprecated values updates
  • Loading branch information
rowanseymour authored Mar 5, 2024
2 parents 1fe91c6 + 4c25a61 commit 5d4656f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
26 changes: 17 additions & 9 deletions excellent/types/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ const serializeDefaultAs = "__default__"
type XObject struct {
baseValue

def XValue
props map[string]XValue
source func() map[string]XValue
marshalDefault bool
def XValue
props map[string]XValue
source func() map[string]XValue

marshalDefault bool
marshalDeprecated bool
}

// NewXObject returns a new object with the given properties
Expand All @@ -41,6 +43,9 @@ func NewXObject(properties map[string]XValue) *XObject {
func NewXLazyObject(source func() map[string]XValue) *XObject {
return &XObject{
source: source,

marshalDefault: false,
marshalDeprecated: true,
}
}

Expand Down Expand Up @@ -94,9 +99,11 @@ func (x *XObject) Format(env envs.Environment) string {
func (x *XObject) MarshalJSON() ([]byte, error) {
marshaled := make(map[string]json.RawMessage, x.Count())
for p, v := range x.properties() {
asJSON, err := ToXJSON(v)
if err == nil {
marshaled[p] = json.RawMessage(asJSON.Native())
if IsNil(v) || x.marshalDeprecated || v.Deprecated() == "" {
asJSON, err := ToXJSON(v)
if err == nil {
marshaled[p] = json.RawMessage(asJSON.Native())
}
}
}

Expand Down Expand Up @@ -205,8 +212,9 @@ func (x *XObject) Default() XValue {
return x.def
}

func (x *XObject) SetMarshalDefault(marshal bool) {
x.marshalDefault = marshal
func (x *XObject) SetMarshalOptions(includeDefault, includeDeprecated bool) {
x.marshalDefault = includeDefault
x.marshalDeprecated = includeDeprecated
}

// Default returns the default value for this
Expand Down
23 changes: 14 additions & 9 deletions excellent/types/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ import (
func TestXObject(t *testing.T) {
env := envs.NewBuilder().Build()

dep1 := types.NewXText("old")
dep1.SetDeprecated("don't use this")

object := types.NewXObject(map[string]types.XValue{
"foo": types.NewXText("abc"),
"bar": types.NewXNumberFromInt(123),
"zed": types.XBooleanFalse,
"dep": dep1,
"xxx": nil,
})
assert.Equal(t, 4, object.Count())
assert.ElementsMatch(t, []string{"foo", "bar", "zed", "xxx"}, object.Properties())
assert.Equal(t, 5, object.Count())
assert.ElementsMatch(t, []string{"foo", "bar", "zed", "dep", "xxx"}, object.Properties())

val, exists := object.Get("foo")
assert.True(t, exists)
Expand All @@ -29,17 +33,17 @@ func TestXObject(t *testing.T) {
assert.False(t, exists)
assert.Nil(t, val)

assert.Equal(t, `{bar: 123, foo: abc, xxx: , zed: false}`, object.Render())
assert.Equal(t, "bar: 123\nfoo: abc\nxxx: \nzed: false", object.Format(env))
assert.Equal(t, `XObject{bar: XNumber(123), foo: XText("abc"), xxx: nil, zed: XBoolean(false)}`, object.String())
assert.Equal(t, `{bar: 123, dep: old, foo: abc, xxx: , zed: false}`, object.Render())
assert.Equal(t, "bar: 123\ndep: old\nfoo: abc\nxxx: \nzed: false", object.Format(env))
assert.Equal(t, `XObject{bar: XNumber(123), dep: XText("old"), foo: XText("abc"), xxx: nil, zed: XBoolean(false)}`, object.String())
assert.Equal(t, "object", object.Describe())

// test marshaling to JSON
asJSON, _ := types.ToXJSON(object)
assert.Equal(t, types.NewXText(`{"bar":123,"foo":"abc","xxx":null,"zed":false}`), asJSON)
assert.Equal(t, types.NewXText(`{"bar":123,"dep":"old","foo":"abc","xxx":null,"zed":false}`), asJSON)

// if there is no explicit default, it's never included
object.SetMarshalDefault(true)
// if there is no explicit default, it's never included, and we can exclude the deprecated property
object.SetMarshalOptions(true, false)
asJSON, _ = types.ToXJSON(object)
assert.Equal(t, types.NewXText(`{"bar":123,"foo":"abc","xxx":null,"zed":false}`), asJSON)

Expand All @@ -48,6 +52,7 @@ func TestXObject(t *testing.T) {
"foo": types.NewXText("abc"),
"bar": types.NewXNumberFromInt(123),
"zed": types.XBooleanFalse,
"dep": types.NewXText("old"),
"xxx": nil,
}))
}
Expand Down Expand Up @@ -102,7 +107,7 @@ func TestXObjectWithDefault(t *testing.T) {
asJSON, _ := types.ToXJSON(object)
assert.Equal(t, types.NewXText(`{"bar":123,"foo":"abc","zed":false}`), asJSON)

object.SetMarshalDefault(true)
object.SetMarshalOptions(true, false)
asJSON, _ = types.ToXJSON(object)
assert.Equal(t, types.NewXText(`{"__default__":"abc-123","bar":123,"foo":"abc","zed":false}`), asJSON)

Expand Down
33 changes: 21 additions & 12 deletions flows/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,28 @@ func (r *Result) Context(env envs.Environment) map[string]types.XValue {
categoryLocalized = r.Category
}

values := types.NewXArray(types.NewXText(r.Value))
values.SetDeprecated("result.values: use value instead")
categories := types.NewXArray(types.NewXText(r.Category))
categories.SetDeprecated("result.categories: use category instead")
categoriesLocalized := types.NewXArray(types.NewXText(categoryLocalized))
categoriesLocalized.SetDeprecated("result.categories_localized: use category_localized instead")

return map[string]types.XValue{
"__default__": types.NewXText(r.Value),
"name": types.NewXText(r.Name),
"value": types.NewXText(r.Value),
"values": types.NewXArray(types.NewXText(r.Value)),
"category": types.NewXText(r.Category),
"categories": types.NewXArray(types.NewXText(r.Category)),
"category_localized": types.NewXText(categoryLocalized),
"categories_localized": types.NewXArray(types.NewXText(categoryLocalized)),
"input": types.NewXText(r.Input),
"extra": types.JSONToXValue(r.Extra),
"node_uuid": types.NewXText(string(r.NodeUUID)),
"created_on": types.NewXDateTime(r.CreatedOn),
"__default__": types.NewXText(r.Value),
"name": types.NewXText(r.Name),
"value": types.NewXText(r.Value),
"category": types.NewXText(r.Category),
"category_localized": types.NewXText(categoryLocalized),
"input": types.NewXText(r.Input),
"extra": types.JSONToXValue(r.Extra),
"node_uuid": types.NewXText(string(r.NodeUUID)),
"created_on": types.NewXDateTime(r.CreatedOn),

// deprecated
"values": values,
"categories": categories,
"categories_localized": categoriesLocalized,
}
}

Expand Down

0 comments on commit 5d4656f

Please sign in to comment.