Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior using 'choose' with SmartObjects #116

Closed
seanmarthur opened this issue Feb 7, 2019 · 1 comment
Closed

Unexpected behavior using 'choose' with SmartObjects #116

seanmarthur opened this issue Feb 7, 2019 · 1 comment
Labels

Comments

@seanmarthur
Copy link

Am I doing something wrong here? I am trying to conditionally display a property based on another property being null.

Example object (just json, but imagine this being a c# DTO):

{
  Assignee: {
    Name: "UserName"
  },
  Group: {
    Name: "GroupName
  }
}

template string: {Assignee:choose(null): {Group.Name}|{Assignee.Name}}

If assignee is null, I want to output group name, otherwise I want to output the assignee name. This seems to work OK in a vacuum. I have some other objects I am adding into the data binding via SmartObjects. When I use both the choose formatter and also try to reference a property from one of the other objects, it will not be able to find that property. Example:

var ctx = new { someOtherProp = "Hello" };
Smart.Format("{Assignee:choose(null): {Group.Name}|{Assignee.Name}} - {someOtherProp}", new SmartObject(new object[] {data, ctx}));

In this example, the output will fail. Could not evaluate the selector "someOtherProp"

When debugging, it seems that after the choose template is processed, the FormattingInfo.CurrentValue loses the reference to the SmartObject, and instead is just set to the data single object. This causes the next token reference to {someOtherProp} to fail, since the object it will search in is no longer the SmartObject.

Is there a better way to do a conditional format like this? For now to work around this I am just going to update my DTO with the 'conditional' property via a getter, but I'd rather not have to do that.

@axunonb axunonb added the Bug label Feb 7, 2019
@axunonb axunonb closed this as completed in bddbfee Feb 8, 2019
@seanmarthur
Copy link
Author

This is perfect - my failing template string immediately began to work.

Thanks for the super fast fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants