github.com/Jeffail/benthos/v3@v3.65.0/template/test/hydration.yaml (about) 1 name: hydration 2 type: processor 3 status: beta 4 categories: [ Utility, Integration ] 5 summary: A common hydration pattern. 6 description: Hydrates content from structured messages based on an ID field. 7 8 fields: 9 - name: cache 10 description: A cache resource to use. 11 type: string 12 - name: id_path 13 description: A dot path pointing to the identifier to use for hydration. 14 type: string 15 - name: content_path 16 description: A dot path pointing to the value to cache and hydrate. 17 type: string 18 19 mapping: | 20 map cache_get { 21 root.branch.request_map = """ 22 root = if this.%v.type() == "null" { 23 this.%v 24 } else { 25 deleted() 26 } 27 """.format(this.content_path, this.id_path) 28 29 root.branch.processors = [ 30 { 31 "cache": { 32 "operator": "get", 33 "resource": this.cache, 34 "key": "${! content() }", 35 } 36 } 37 ] 38 39 root.branch.result_map = "root.%v = content().string()".format(this.content_path) 40 } 41 42 map cache_set { 43 root.branch.request_map = """ 44 meta id = this.%v 45 root = this.%v | deleted() 46 """.format(this.id_path, this.content_path) 47 48 root.branch.processors = [ 49 { 50 "cache": { 51 "operator": "set", 52 "resource": this.cache, 53 "key": """${! meta("id") }""", 54 "value": "${! content() }", 55 } 56 } 57 ] 58 } 59 60 root.try = [ 61 this.apply("cache_set"), 62 this.apply("cache_get"), 63 ] 64 65 # The following is only used for testing config field type coercion 66 let cache_type = this.cache.type() 67 let id_type = this.id_path.type() 68 let content_type = this.content_path.type() 69 root = if $cache_type != "string" || $id_type != "string" || $content_type != "string" { 70 throw("Fields were coerced into incorrect types: cache(%v), id_path(%v), content_path(%v)".format($cache_type, $id_type, $content_type)) 71 } 72 73 tests: 74 - name: Basic fields 75 config: 76 cache: foocache 77 id_path: article.id 78 content_path: article.content 79 80 expected: 81 try: 82 - branch: 83 request_map: |-2 84 85 meta id = this.article.id 86 root = this.article.content | deleted() 87 88 processors: 89 - cache: 90 operator: set 91 resource: foocache 92 key: ${! meta("id") } 93 value: ${! content() } 94 95 - branch: 96 request_map: |-2 97 98 root = if this.article.content.type() == "null" { 99 this.article.id 100 } else { 101 deleted() 102 } 103 104 processors: 105 - cache: 106 operator: get 107 resource: foocache 108 key: ${! content() } 109 result_map: root.article.content = content().string() 110 111 - name: Type coercion 112 config: 113 cache: 10 114 id_path: false 115 content_path: 20.475