3

enter image description hereHi i am getting an error saying Invalid input ' ', expected '}' for the do block. I am trying to create a concatenated String output

fun getStreetAddress(address1 ,address2) = 
do {
var addr1 = address1
var addr2 = address2
var finalAddress = ""
---
if(null != address1 and sizeOf(address1>30)) 
addr1 = address1[0 to 30]
if(null != address2 and sizeOf(address2>30)) 
addr2 = address2[0 to 30]
finalAddress = "$(addr1) $(addr2)"
}

2 Answers 2

4

You are using variables incorrectly. You can't assign a value to variable in the body of the block. Only in the definition part, before the --- separator. Remember that DataWeave is a functional language, not an imperative one.

Try returning the result of the scores directly. You can also use if() as a function.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry , i am not following 100% . Would you able to make the code change to make it running .
I'm not I'm front of a computer jus a phone, I can't test it. It should be something like ---(if (cond1) address1[0 to 30] else "" ) ++ (if (cond2) ...)
2

You can expand it all out and not use variables at all with something like

    fun getStreetAddress(address1 ,address2) = 
    (if(sizeOf(address1 default "") >30) 
        address1[0 to 30]
        else address1) 
    ++ " " ++ 
    (if(sizeOf(address2 default "") >30) 
        address2[0 to 30]
        else address2)
---

getStreetAddress("asdf","1234567890123456789012345678901234567890")

If you really are wanting to assign variables inside your function you can break up the logic a bit. The important thing to remember is everything below the --- is the return statement for the function.

%dw 2.0
output application/json
     fun getStreetAddress(address1,address2) = do{
        var add1 = (if(sizeOf(address1 default "") >10) 
            address1[0 to 10]
            else address1) default ""

        var add2 = (if(sizeOf(address2 default "") >10) 
            address2[0 to 10]
            else address2) default ""
        ---
        add1 ++ " " ++ add2 
        }
---
    
getStreetAddress("1234567890123",null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.