0
votes

I have defined following YAML properties file in Mule.

YAML properties File

table:
  "customers": 
    name: customerID
  "orders": 
    name: ordersDateMin,orderDateMax

I know that to get the name I should use "#[p('table.customers.name')].

What I want to know, is there any way to get the key by passing .name, something like table.key or table.$ . I mean if I pass customerID, I should get the key "customers". Similarly if I pass orderDateMin,orderDateMax it should return "orders".

Is it possible?

Thanks in advance

1
OK I think I understand what you are asking. There is no easy way to do a reverse lookup of a property value and get the field(s). What you can do is read the file using a DW expression like this: readUrl("classpath://properties.yaml","application/yaml") and then write code to search for the value and get the field(s).George
Thank you. Will try thatuser12277274
I did amend the answer below to reflect my newfound understanding of your question :). Just look at the code below and see if is it what you need.George
Why not just create another map, with the possible ".names" that map to their respective keys in the table map.Zingers

1 Answers

0
votes

Try this:

%dw 2.0
output application/dw
var props = readUrl("classpath://properties.yaml","application/yaml")
var v = "customerID"

var leafsReversed = (o: Object, r: Object = {}) -> o match {
    case is Object -> r ++ (
        $ mapObject ( 
            (v,k,i) -> if (v["name"] is String) ({(v["name"]):k}) else leafsReversed(v,r)
        )
    )
    else -> r
}

---
[props,leafsReversed(props)]

This is a non-trivial problem with a non-trivial solution. leafsReversed is a recursive function that looks for <field>.name: <value> sequences and tests if the value is a string, if it is then it will create a key,value pair of <value>: <field>. Now you have a map and you can perform lookups.

My advise to you is find another way.

Your property file content is malformed, it does not work as presented above, I changed it to the following for my tests:

table:
  customers: 
    name: "customerID"
  orders: 
    name: "ordersDateMin,orderDateMax"

Here's the Mule Configuration file contents:

<?xml version="1.0" encoding="UTF-8"?>

    <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
        xmlns="http://www.mulesoft.org/schema/mule/core"
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
        <configuration-properties doc:name="Configuration properties" doc:id="4683e6d8-ad4d-453b-9f1c-19949241c336" file="properties.yaml" />
        <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="dd84777d-25b1-4e42-870b-8e7745689041" >
            <http:listener-connection host="0.0.0.0" port="8081" />
        </http:listener-config>
        <flow name="yaml-propsFlow1" doc:id="62367531-86df-4383-ac60-294a3ae58043" >
            <http:listener doc:name="Listener" doc:id="b1aae490-cfbb-49d9-8216-33e55607f1f2" config-ref="HTTP_Listener_config" path="/"/>
            <ee:transform doc:name="Transform Message" doc:id="63e74066-128f-464d-94f3-646872dfbb6f" >
                <ee:message >
                    <ee:set-payload ><![CDATA[%dw 2.0
    output application/json
    var props = readUrl("classpath://properties.yaml","application/yaml")
    var v = "customerID"
    
    var leafsReversed = (o: Object, r: Object = {}) -> o match {
        case is Object -> r ++ (
            $ mapObject ( 
                (v,k,i) -> if (v["name"] is String) ({(v["name"]):k}) else leafsReversed(v,r)
            )
        )
        else -> r
    }
    
    ---
    [props,leafsReversed(props)]]]></ee:set-payload>
                </ee:message>
            </ee:transform>
        </flow>
    </mule>