connect icon indicating copy to clipboard operation
connect copied to clipboard

Mapping message to a `string` causes error

Open krazylek opened this issue 3 years ago • 3 comments

Using a simple mapping pipeline like this:

input:
  generate:
    count: 1
    interval: 0s
    mapping: |
      root = { "hello": "world" }

pipeline:
  processors:
    - mapping: |
        root = "test"
    - log:
        level: INFO
        message: '${! this }'

Will generate the correct output, but the log processor will fail with a Message interpolation error: context was undefined error. Here the error is actually caused by the log, but the underlying problem would also make a branch processor fail:

input:
  generate:
    count: 1
    interval: 0s
    mapping: |
      root = { "hello": "world" }

pipeline:
  processors:
    - branch:
        processors:
          - mapping: |
              root = "test"
        result_map: |
          root.test_value = this

It will cause a Branch error: result mapping failed: failed assignment and test_value won't be set.

Last insight, it happens whatever the source input component is, and for all type of message. From what I see, even from a string! Example:

input:
  generate:
    count: 1
    interval: 0s
    mapping: |
      root = "hello world"

pipeline:
  processors:
    - mapping: |
        root = this

Here I get a failed assignment (line 1): unable to reference message as structured (with 'this'): parse as json: invalid character 'h' looking for beginning of value". Please note that the 'h' mentioned is the first letter in the "hello world" input.

krazylek avatar Mar 01 '23 02:03 krazylek

I think the issue is: this is common used when handling json like messages:

https://www.benthos.dev/docs/guides/bloblang/about#trouble-shooting

you can substitute this by content() and it should works

pipeline:
  processors:
    - mapping: |
        root = "test"
    - log:
        level: INFO
        message: '${! content() }'

BTW content() seems to works in all cases, while this is more restrict

peczenyj avatar Mar 01 '23 08:03 peczenyj

Yes indeed, that makes sense... I guess this automatic parsing brings some nice magic in some cases!

Also my branch example would be solved with the content() solution:

pipeline:
  processors:
    - branch:
        processors:
          - mapping: |
              root = "test"
        result_map: |
          root.test_value = content()

Gives me the expected output.

Thank you for you help and sorry for the false issue!

krazylek avatar Mar 01 '23 10:03 krazylek

Perhaps we can use this issue to improve the error message. the content is really cryptic

peczenyj avatar Mar 01 '23 11:03 peczenyj