Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слева Предыдущая версия
Следующая версия
Предыдущая версия
develop:mule:dataweave [2024/02/25 12:59] – [Condition] mirocowdevelop:mule:dataweave [2024/03/06 22:19] (текущий) – [Ссылки] mirocow
Строка 18: Строка 18:
 </code> </code>
  
 +<note tip>IDE https://dataweave.mulesoft.com/learn/playground</note>
 +===== Oprators =====
 +
 +  * using: For initializing local variables in a DataWeave script.
 +  * Unary DataWeave operators at this level:
 +    * .^: Schema selector.
 +    * .#: Namespace selector.
 +    * ..: Descendants selector. 
 +    * not: Logical operator for negation.
 +    * .@: All attribute selector, for example, in a case that uses the expression payload.root.a.@ to return the attributes and values of the input payload <root> <a attr1="foo" attr2="bar" /></root> within the array of objects { "attr1": "foo", "attr2": "bar" }.                                                                                                                                                                                                                                              
 ==== Condition ==== ==== Condition ====
  
Строка 151: Строка 161:
 } }
 </code> </code>
-===== DataWeave Reference =====+ 
 +=== Логические операторы / Logical Operators ==
 + 
 +  * A > B Greater than 
 +  * A < B Less than 
 +  * A >= B Greater than or equal to 
 +  * A <= B Less than or equal to 
 +  * A == B Equal to 
 +  * A != B Not equal to 
 +  * A ~= B Similar to 
 +  * not A Logical negation 
 +  * !A Logical negation 
 +  * A and B Logical and 
 +  * A or B Logical or 
 +  * not Negates the result of the input. See also, !. 
 +  * ! Negates the result of the input. See also, not. Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later. 
 +  * and Returns true if the result of all inputs is true, false if not. 
 +  * or Returns true if the result of any input is true, false if not. 
 + 
 +<code> 
 +%dw 2.0 
 +output json 
 +var age = 2 
 +--- 
 +age < 10 
 +</code> 
 + 
 +<code> 
 +%dw 2.0 
 +var myArray = [1,2,3,4,5] 
 +var myMap = myArray map not (($ mod 2) == 0) 
 +output application/json 
 +--- 
 +
 +  "not" : [ 
 +    "notTrue" : not true, 
 +    "notFalse" : not false, 
 +    "myMapWithNot" : myMap 
 +  ], 
 +  "and" : [ 
 +    "andTrueFalse" : true and false, 
 +    "andIsTrue" : (1 + 1 == 2) and (2 + 2 == 4), 
 +    "andIsFalse" : (1 + 1 == 2) and (2 + 2 == 2) 
 +  ], 
 +  "or" : [ 
 +    "orTrueFalse" : true or false, 
 +    "orIsTrue" : (1 + 1 == 2) or (2 + 2 == 2), 
 +    "orIsFalse" : (1 + 1 == 1) or (2 + 2 == 2) 
 +  ], 
 +  "!-vs-not" : [ 
 +      "example-!" : (! true or true), 
 +      "example-not" : (not true or true) 
 +  ] 
 +
 +</code> 
 + 
 +<code> 
 +%dw 2.0 
 +var orNot = if (1 + 1 == 4 or not 1 == 2) {"answer": "orNot - Condition met"
 +             else {"answer": "nope"
 +var andNot = if (1 + 1 == 2 and not 1 == 2) {"answer": "andNot - Condition met"
 +             else {"answer": "nope"
 +var notWithAndNot = if (not (1 + 1 == 2 and not 1 == 1)) {"answer": "notWithAndNot - Condition met"
 +              else {"answer": "nope"
 +output application/json 
 +--- 
 +{ "answers"
 +  [ 
 +    orNot, 
 +    andNot, 
 +    notWithAndNot 
 +  ] 
 +
 +</code> 
 + 
 +=== Types === 
 + 
 +<code> 
 +%dw 2.0 
 +output application/json 
 +--- 
 +
 +  /* 
 +   * A multi-line 
 +   * comment here. 
 +   */ 
 +  myString: "hello world", 
 +  myNumber: 123, 
 +  myFloatingPointNumber: 123.456, 
 +  myVeryBigNumber: 12341234134123412341234123, 
 +  myDate: |2018-12-07|, 
 +  myTime: |11:55:56|, 
 +  myDateTime: |2018-10-01T23:57:59-03:00|, 
 +  myBoolean: true, 
 +  myArray: [ 1, 2, 3, 5, 8], 
 +  myMixedArray: [ 1, 2, "blah", { hello: "there" } ], 
 +  myObjectKeyValuePair: { innerKey: "innerValue" }, 
 +  myObjectWithConditionalField: { a : { b : 1, ( c : 2 ) if true, (d : 4) if false } }, 
 +  myNull: null, 
 +  myBinary: "abcd1234123" as Binary 
 +  //A one-line comment here. 
 +
 +</code> 
 +===== dw::* =====
  
 ==== dw::core::Arrays / Working with Array ==== ==== dw::core::Arrays / Working with Array ====
Строка 175: Строка 288:
   * **takeWhile** Selects elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition.   * **takeWhile** Selects elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition.
  
 +=== divideBy ===
 +
 +<code>
 +%dw 2.0
 +import divideBy from dw::core::Objects
 +output application/json
 +---
 +{ "a": 1, "b": true, "a": 2, "b": false, "c": 3 } divideBy 2 
 +</code>
 +=== drop ===
 +
 +<code>
 +%dw 2.0
 +import * from dw::core::Arrays
 +var samplearray = ["124", "35454", "Sachin","Amit"]
 +output application/json
 +---
 + 
 +drop(samplearray, 2)
 +</code>
 +
 +=== indexOf ===
 +
 +<code>
 +%dw 2.0
 +import * from dw::core::Arrays
 +var samplearray = ["124", "35454", "Sachin","Amit"]
 +output application/json
 +---
 + 
 +indexOf(samplearray, "Sachin")
 +</code>
 +
 +=== join ===
 +
 +<code>
 +%dw 2.0
 +import * from dw::core::Arrays
 + 
 +var items = [{id: "12", name:"Tea"},{id: "22", name:"Salt"},{id: "3", name:"Soap"},{id: "5", name:"Butter"}]
 + 
 +var Prices = [{Id: "12", price:123},{Id: "3", price:24}, {Id: "22", price:20}, {Id: "4", price:456}]
 + 
 +output application/json
 +---
 +join(items, Prices, (item) -> item.id, (price) -> price.Id)
 +
 +</code>
 +=== takeWhile ===
 +
 +
 +<code>
 +%dw 2.0
 +import * from dw::core::Arrays
 +output application/json
 +var arr = [1,2,3,4,1,2]
 +---
 + 
 +arr takeWhile $ &lt;= 2
 +
 +</code>
 +
 +<code>
 +%dw 2.0
 +import * from dw::core::Objects
 +output application/json
 +var obj = {
 +  "a": 1,
 +  "b": 2,
 +  "c": 5,
 +  "d": 1
 +}
 +---
 +obj takeWhile ((value, key) ->  value < 3)
 +</code>
 ==== dw::Core ==== ==== dw::Core ====
  
Строка 188: Строка 376:
 --- ---
 payload distinctBy $.id payload distinctBy $.id
 +</code>
 +
 +<code>
 +%dw 2.0
 +output application/json
 +var str = [1,2,3,4,5,6,7]
 +var str1= [4,5,6,7,1]
 +---
 +(str ++ str1) distinctBy ((item, index) ->item )
 </code> </code>
  
Строка 455: Строка 652:
 </code> </code>
  
 +<code>
 +%dw 2.0
 +output application/json  
 +---
 +payload filterObject ((value, key) -> 
 +    (key as String != "first name"
 +    and (key as String != "last name")
 +)
 +</code>
 === flatten === === flatten ===
  
Строка 573: Строка 779:
 </code> </code>
  
 +<code>
 +%dw 2.0
 +output application/java
 +fun process(obj: Object) = obj mapObject ((value, key) -> {
 +    (key): key match {
 +        case "description" -> vars.descriptionValue
 +        else -> value
 +    }
 +})
 +---
 +payload map ((item, index) -> 
 +    process(item)
 +)
 +</code>
 === mapObject === === mapObject ===
  
Строка 726: Строка 946:
  
 So result will be 9 So result will be 9
 +</code>
 +
 +<code>
 +%dw 2.0
 +output application/json
 +
 +var data = {"xyz": {"abc":["account":[{"value":"savings"}]]}}
 +---
 +data..value reduce $
 </code> </code>
  
Строка 883: Строка 1112:
   * **valueSet** Returns an array of the values from key-value pairs in an object.   * **valueSet** Returns an array of the values from key-value pairs in an object.
  
 +=== everyEntry ===
 +
 +<code>
 +%dw 2.0
 +import everyEntry from dw::core::Objects
 +output application/json
 +---
 +{
 +    a: {} everyEntry (value, key) -> value is String,
 +    b: {a: "", b: "123"} everyEntry (value, key) -> value is String,
 +    c: {a: "", b: 123} everyEntry (value, key) -> value is String,
 +    d: {a: "", b: 123} everyEntry (value, key) -> key as String == "a",
 +    e: {a: ""} everyEntry (value, key) -> key as String == "a",
 +    f: null everyEntry ((value, key) -> key as String == "a")
 +}
 +</code>
 +
 +=== mergeWith ===
 +
 +<code>
 +%dw 2.0
 +import mergeWith from dw::core::Objects
 +output application/json
 +---
 +{ "a" : true, "b" : 1} mergeWith { "a" : false, "c" : "Test"
 +</code>
 +
 +=== someEntry ===
 +
 +<code>
 +%dw 2.0
 +import someEntry from dw::core::Objects
 +output application/json
 +---
 +{
 +    a: {} someEntry (value, key) -> value is String,
 +    b: {a: "", b: "123"} someEntry (value, key) -> value is String,
 +    c: {a: "", b: 123} someEntry (value, key) -> value is String,
 +    d: {a: "", b: 123} someEntry (value, key) -> key as String == "a",
 +    e: {a: ""} someEntry (value, key) -> key as String == "b",
 +    f: null someEntry (value, key) -> key as String == "a"
 +}
 +</code>
 ==== dw::util::Values ==== ==== dw::util::Values ====
  
Строка 1019: Строка 1291:
  
   * https://docs.mulesoft.com/dataweave/latest/   * https://docs.mulesoft.com/dataweave/latest/
 +  * https://mulesy.com/1-mulesoft-as-middleware-solution/
 +  * https://reddeveloper.ru/tags/mule
 +  * https://questu.ru/tag/mule/