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

ReQL proposal: string templating #3353

Open
marshall007 opened this issue Nov 19, 2014 · 5 comments · May be fixed by #7066
Open

ReQL proposal: string templating #3353

marshall007 opened this issue Nov 19, 2014 · 5 comments · May be fixed by #7066
Assignees
Milestone

Comments

@marshall007
Copy link
Contributor

String building is a bit of a pain right now, at least in the JS driver. This is loosely related to #3048.

r.table('foo').map({
  id: r.row('id'),
  bars: r.http(r.add(r.args(['http://example.com/foos/', r.row('id'), '/bars?type=', r.row('type')])))
})

It would be nice to have something like this:

r.table('foo').map({
  id: r.row('id'),
  bars: r.http(r.fmt('http://example.com/foos/{id}/bars?type={type}', r.row))
})
@srh
Copy link
Contributor

srh commented Nov 19, 2014

If this feature is added, for obvious reasons it should mandate that the first argument to fmt is a string literal, not some other expression that may return a string, not an r.args expression or other expression.

@danielmewes
Copy link
Member

Thank you for the suggestion @marshall007 . This looks useful.
I don't think we'll get to it before our 2.0 release, but we'll keep it on our list.

@danielmewes danielmewes added this to the backlog milestone Nov 19, 2014
@thelinuxlich
Copy link

very nice!

@marshall007
Copy link
Contributor Author

Here's a quick and dirty client-side implementation I've been using in case anyone else finds it useful:

r.fmt = function (template, params) {
  var parts = template.split(/(\{\w+\})/g).map(function (part) {
    if (part.indexOf('{') === 0) {
      var name = part.slice(1, -1);
      return params(name).coerceTo('string');
    }
    return part;
  });

  return r.add(r.args(parts));
};

Example usage:

r.fmt('http://example.com/foos/{id}/bars?type={type}', r.expr({
  id: 123,
  type: 'tag'
}))
// => "http://example.com/foos/123/bars?type=tag"

@danielmewes
Copy link
Member

@marshall007 That's a very neat work-around!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Review
Development

Successfully merging a pull request may close this issue.

5 participants