Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Extraneous double-quotes rendered in 'foreach' Ergo Expression #768

Closed
martinhalford opened this issue Aug 14, 2020 · 6 comments
Closed

Comments

@martinhalford
Copy link
Member

Describe the bug
Ergo foreach renders extra double-quotes within a grammer.tem.md

To Reproduce
Create an asset such as follows:

asset ExclusiveSaleAuthorityContract extends AccordContract {
  ...
  o Commission[] commissions
}

Model the Commission as follows:

concept Commission {
  o Double percentAmount
  o Double dollarAmount
  o Double price
  o String description
}

In the data.json file, have an array of commissions like follows...

  "commissions" : [
    {
      "$class" : "systems.benext.reiv.Commission",
      "percentAmount" : 7,
      "dollarAmount" : 35000,
      "price" : 1650000,
      "description" : "7% of the property sale between $0 and $500,000"
    },
    {
      "$class" : "systems.benext.reiv.Commission",
      "percentAmount" : 7.0,
      "dollarAmount" : 20000,
      "price" : 1500000,
      "description" : "8% of the property sale between $500,000 and $1,000,000"
    }
  ]

Create the following Ergo expression in a grammar.tem.md...

{{%
    foreach c in commissions
    return "$ " ++ c.dollarAmount as "0,0" ++ "\n"
%}}
{{%
    foreach c in commissions
    return c.percentAmount as "0,0.0" ++ " percent" ++ "\n"
%}}
{{%
    foreach c in commissions
    return "$ " ++ c.price as "0,0" ++ "\n"
%}}
{{%
    foreach c in commissions
    return c.description ++ "\n"
%}}

You will get something similar to the following:

$ 20,000
""$ 35,000
7.0 percent
""7.0 percent
$ 1,500,000
""$ 1,650,000
7% of the property sale between $0 and $500,000
""8% of the property sale between $500,000 and $1,000,000

Expected behavior

Expect no extra "" quote marks.

$ 20,000
$ 35,000
7.0 percent
7.0 percent
$ 1,500,000
$ 1,650,000
7% of the property sale between $0 and $500,000
8% of the property sale between $500,000 and $1,000,000

Additional context
Cicero 0.21.3

@jeromesimeon
Copy link
Member

jeromesimeon commented Aug 14, 2020

Some more data on this.

  1. Ergo returns the proper array of strings

Screenshot 2020-08-14 at 9 42 06 AM

  1. Those quotes are likely an artefact (i.e., bug) when arrays are turned into text in the context where Ergo is used in template formulas (should this be be considered a type error?)

  2. You should be able to fix your code to avoid that issue by constructing the string result yourself. e.g.,:

{{%
    join("\n", foreach c in commissions return "$ " ++ c.dollarAmount as "0,0")
%}}

@jeromesimeon
Copy link
Member

jeromesimeon commented Aug 14, 2020

  1. I think this code would look nicer written as a join template block:
{{#join commissions separator=""}}$ {{dollarAmount as "0,0"}}
{{/join}}

@jeromesimeon
Copy link
Member

Example in template studio. With this grammar:
Screenshot 2020-08-14 at 9 58 36 AM

Get this drafted text:
Screenshot 2020-08-14 at 9 59 16 AM

@martinhalford
Copy link
Member Author

Thanks Jerome. I'll try the join operator, as suggested.

@jeromesimeon
Copy link
Member

Should be fixed in #776 . Here is a test with volume discount. For the following grammar:

bash-3.2$ more text/grammar.tem.md
#### Discount.

The Discount is determined according to the following table:
{{#ulist rates}}
{{volumeAbove}}$ million <= Volume < {{volumeUpTo}}$ million : {{rate}}%
{{/ulist}}

{{%
foreach r in rates return
"[rate is " ++ toString(r.rate) ++ "] "
%}}

Before the fix:

bash-3.2$ cicero draft 
#### Discount.

The Discount is determined according to the following table:
-  0.0$ million <= Volume < 1.0$ million : 3.1%
-  1.0$ million <= Volume < 10.0$ million : 3.1%
-  10.0$ million <= Volume < 50.0$ million : 2.9%
-  50.0$ million <= Volume < 500.0$ million : 2.5%
-  500.0$ million <= Volume < 1000.0$ million : 1.2%
-  1000.0$ million <= Volume < 1000000.0$ million : 0.1%

{{%"[rate is 0.1] ""[rate is 1.2] ""[rate is 2.5] ""[rate is 2.9] ""[rate is 3.1] ""[rate is 3.1] "%}}

After the fix:

bash-3.2$ ~/git/cicero/packages/cicero-cli/index.js draft 
#### Discount.

The Discount is determined according to the following table:
-  0.0$ million <= Volume < 1.0$ million : 3.1%
-  1.0$ million <= Volume < 10.0$ million : 3.1%
-  10.0$ million <= Volume < 50.0$ million : 2.9%
-  50.0$ million <= Volume < 500.0$ million : 2.5%
-  500.0$ million <= Volume < 1000.0$ million : 1.2%
-  1000.0$ million <= Volume < 1000000.0$ million : 0.1%

{{%[rate is 0.1] [rate is 1.2] [rate is 2.5] [rate is 2.9] [rate is 3.1] [rate is 3.1] %}}

This fix will be released in Cicero 0.21.4

@jeromesimeon
Copy link
Member

Cicero 0.21.4 has been published with a fix for this. Closing this as resolved. Please reopen (or open a new issue) if further problems.

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

No branches or pull requests

2 participants