chongkong / glu

Simple config file generation command line tool
Other
7 stars 0 forks source link

Additional Operations #1

Open chongkong opened 7 years ago

chongkong commented 7 years ago

Currently supported operations are

  1. String interpolation
  2. Scope injection using <
  3. String filtering using |
  4. Fallback from undefined using ??

Although combinations of above operations can structure many of the configuration, there is some limitations to achieve everything. This issue is a small proposal for future improvements of operations

List Operation

Merging two lists

{
  "plus": "{{ first + second }}",
  "colons": "{{ first::second }}",
  "array_unpack": ["*{{ first }}", "*{{ second }}" ]
}

Mapping injection with list of scope (really required?)

{
  "scopes": [
    { "name": "foo" },
    { "name": "bar" },
    { "name": "cor" },
    { "name": "giz" }
  ]
}

{
  "greeting": "hello, {{ name }}",
  "greetings": "{{ greeting << scopes }}",
  "greetings2": [
    "{% for scope in scopes %}",
    "{{ greeting < scope }}",
    "{% end for %}"
  ]
}

Unique filter

{
   "duplicated": [1, 1, 2, 3, 3, 3],
   "unqiue" : "{{ duplicated | unique }}"
}

Dictionary

Merging two dictionary (really required?)

{
  "plus": "{{ first + second }}",
  "unpack": [
    "**{{ this_is_weird }}",
    "**{{ looks_like_an_array }}"
  ]
}

Integer Operation

Simple integer operation

{
   "one": 1,
   "two": "{{ one + 1 }}",
   "three": "{{ one * 3 }}",
   "four": "{{ two * two }}",
   "five": "{{ 10 / two }}"
}
chongkong commented 7 years ago

I think adding +, -, *, / operators and defining operation priority seems the clearest.

But I also want some kind of list-comprehension-like syntax, like

[
  "{% for scope in scopes %}",
  "{{ greeting < scope }}",
  "{% end for %}"
]

or

{
  "for_comprehension": "{{ foo.bar for foo in foos }}",
  "map": "{{ foos map bar }}",
  "dot_operator": "{{ foos.bar }}",
  "dot_operator_with_other": [
    "{{ foos .+ 1 }}",
    "{{ foos .?? fallback }}",
    "{{ foos .< scopes }}"
  ]
}