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

Provide method of escaping values containing lookup identifiers #14

Open
mdeltito opened this issue May 26, 2021 · 5 comments
Open

Provide method of escaping values containing lookup identifiers #14

mdeltito opened this issue May 26, 2021 · 5 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@mdeltito
Copy link
Member

This is an edge case (and there are ways around it) but consider the following example of a value that starts with a #:

function myAction(opts) {
  return this.lookup(opts)
}

chain
  .myAction({
    id: '!random'
  , color: '#cccccc' 
  })

A couple of options offhand would be:

  • Introduce a control character to escape e.g. \#foo, \!bar
  • Provide a built-in function that just reflects the value e.g. !literal("#ccc")
    • At minimum this is a viable workaround, since quoted function arguments do not pass through lookup
@mdeltito mdeltito added the enhancement New feature or request label May 26, 2021
@esatterwhite
Copy link
Member

I like the control character from a usability perspective. but would probably need to be added to the parser

@mdeltito mdeltito self-assigned this Jun 18, 2021
@esatterwhite
Copy link
Member

esatterwhite commented Jul 6, 2021

was poking at this a bit. The parser has the concept of modes which define a set of valid tokens that can be lexed.
The tricky bit with that is that tokens defines the start and end char for that mode.

We do this in a round about way with the String token - anything between quotes.

The simple case of an escape char would need a closing char if we wanted to use the high level API

createToken({
  name: 'escapestart'
, pattern: /\/-/
, push_mode: 'literal'
})

createToken({
  name: 'escapestop'
, pattern: /-\//
, pop_mode: 'literal'
})
defaults = {
  color: '\#cccccc\'
}

At this point, its not much better than quoting it

defaults = {
  color: '"#cccccc"'
}

The idea of a literal function is interesting, but thats an even longer way of quoting (our current literal mechanism)

defaults = {
  color: '!literal:#cccccc'
}
class Chain {
  $literal(value) {
    if (value == null) return ''
    return `"${value}"`
  }
}

Another thing to consider is where the sane logical boundaries. Can the escape character be used anywhere?

!fn(\#cccccc, foo, \!bar)

@esatterwhite
Copy link
Member

or rather than a parsing problem maybe its just a logical lookup problem

class Chain {
  lookup(value) {
    if (typeOf(value) === 'string' && value.startsWith('\\')) {
      return value.replace('\\', '')
    }
  }
}

@mdeltito
Copy link
Member Author

mdeltito commented Jul 6, 2021

@esatterwhite I took a brief look at this as well, and that's sort of where I landed too

https://github.com/logdna/setup-chain-node/compare/mdeltito/issue-14?expand=1#diff-36d58ae41c10a07cafe494d25b0180f2eb3fec642bfb63feced95e2b012b5196R117-R119

The escape char is included in the tokens there, but it doesn't need to be. Moving it to strictly a lookup concern might solve the remaining test case too.

@esatterwhite
Copy link
Member

esatterwhite commented Jul 6, 2021

Actually I think we could define this as a subrule.

  • [escape]<lookup>
  • [escape]<fn>

Then the escape is a child of the lookup fn.

if (fn.children[0].escape) {
  return this.wrapLiteral(fn.children.slice(1))
}

@esatterwhite esatterwhite added good first issue Good for newcomers help wanted Extra attention is needed labels Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants