name: hydration type: processor status: beta categories: [ Utility, Integration ] summary: A common hydration pattern. description: Hydrates content from structured messages based on an ID field. fields: - name: cache description: A cache resource to use. type: string - name: id_path description: A dot path pointing to the identifier to use for hydration. type: string - name: content_path description: A dot path pointing to the value to cache and hydrate. type: string mapping: | map cache_get { root.branch.request_map = """ root = if this.%v.type() == "null" { this.%v } else { deleted() } """.format(this.content_path, this.id_path) root.branch.processors = [ { "cache": { "operator": "get", "resource": this.cache, "key": "${! content() }", } } ] root.branch.result_map = "root.%v = content().string()".format(this.content_path) } map cache_set { root.branch.request_map = """ meta id = this.%v root = this.%v | deleted() """.format(this.id_path, this.content_path) root.branch.processors = [ { "cache": { "operator": "set", "resource": this.cache, "key": """${! meta("id") }""", "value": "${! content() }", } } ] } root.try = [ this.apply("cache_set"), this.apply("cache_get"), ] # The following is only used for testing config field type coercion let cache_type = this.cache.type() let id_type = this.id_path.type() let content_type = this.content_path.type() root = if $cache_type != "string" || $id_type != "string" || $content_type != "string" { throw("Fields were coerced into incorrect types: cache(%v), id_path(%v), content_path(%v)".format($cache_type, $id_type, $content_type)) } tests: - name: Basic fields config: cache: foocache id_path: article.id content_path: article.content expected: try: - branch: request_map: |-2 meta id = this.article.id root = this.article.content | deleted() processors: - cache: operator: set resource: foocache key: ${! meta("id") } value: ${! content() } - branch: request_map: |-2 root = if this.article.content.type() == "null" { this.article.id } else { deleted() } processors: - cache: operator: get resource: foocache key: ${! content() } result_map: root.article.content = content().string() - name: Type coercion config: cache: 10 id_path: false content_path: 20.475