Различия
Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
develop:mule:dataweave [2024/02/25 12:45] – [filterObject] mirocow | develop:mule:dataweave [2024/03/06 22:19] (текущий) – [Ссылки] mirocow | ||
---|---|---|---|
Строка 17: | Строка 17: | ||
payload | payload | ||
</ | </ | ||
+ | |||
+ | <note tip>IDE https:// | ||
+ | ===== 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 < | ||
+ | ==== Condition ==== | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myVar = { country : " | ||
+ | output application/ | ||
+ | --- | ||
+ | if (myVar.country == " | ||
+ | { currency: " | ||
+ | else { currency: " | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var user = {" | ||
+ | --- | ||
+ | name : user mapObject { | ||
+ | (' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var aRecord = | ||
+ | [ | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | output application/ | ||
+ | --- | ||
+ | { examples: | ||
+ | { | ||
+ | ex1 : if (1 + 1 == 55) true | ||
+ | else false, | ||
+ | ex2 : if ([1, | ||
+ | else "value of index 1 is not 1", | ||
+ | ex3 : if (isEmpty(aRecord.bookId)) "ID is empty" | ||
+ | else "ID is not empty", | ||
+ | ex4 : aRecord.bookId map (idValue) -> | ||
+ | if (idValue as Number == 101) idValue as Number | ||
+ | else "not 101" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var user = {" | ||
+ | --- | ||
+ | name : user filter ($ contains " | ||
+ | </ | ||
+ | |||
+ | ==== Filters ==== | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Functions ==== | ||
+ | |||
+ | * Named functions | ||
+ | * Lambdas | ||
+ | * Passing functions to other functions | ||
+ | * Calling 2-arity functions with infix notation | ||
+ | * $, $$, $$$ syntax | ||
+ | |||
+ | === Лямда функции / Functions as Values === | ||
+ | |||
+ | Полезность лямбд становится очевидной, | ||
+ | |||
+ | * Лямбды — это значения, | ||
+ | * Значения могут передаваться функциям в качестве аргументов, | ||
+ | |||
+ | В DataWeave функции и лямбды (анонимные функции) могут передаваться как значения или присваиваться переменным. | ||
+ | |||
+ | DataWeave предоставляет несколько способов создания функций. Точно так же, как у нас есть именованные функции, | ||
+ | |||
+ | <note tip> | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | |||
+ | fun isOddNum(n) = | ||
+ | (n mod 2) == 1 | ||
+ | |||
+ | // Generate [1, 2, ..., 5] | ||
+ | var numbers = (1 to 5) | ||
+ | --- | ||
+ | filter(numbers, | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | |||
+ | var numbers = (1 to 5) | ||
+ | --- | ||
+ | filter(numbers, | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | (() -> 2 + 3)() | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var toUser = (user) -> { | ||
+ | firstName: user.field1, | ||
+ | lastName: user.field2 | ||
+ | } | ||
+ | --- | ||
+ | { | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Логические операторы / 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. | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | var age = 2 | ||
+ | --- | ||
+ | age < 10 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myArray = [1,2,3,4,5] | ||
+ | var myMap = myArray map not (($ mod 2) == 0) | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var orNot = if (1 + 1 == 4 or not 1 == 2) {" | ||
+ | else {" | ||
+ | var andNot = if (1 + 1 == 2 and not 1 == 2) {" | ||
+ | else {" | ||
+ | var notWithAndNot = if (not (1 + 1 == 2 and not 1 == 1)) {" | ||
+ | else {" | ||
+ | output application/ | ||
+ | --- | ||
+ | { " | ||
+ | [ | ||
+ | orNot, | ||
+ | andNot, | ||
+ | notWithAndNot | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Types === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | /* | ||
+ | * A multi-line | ||
+ | * comment here. | ||
+ | */ | ||
+ | myString: "hello world", | ||
+ | myNumber: 123, | ||
+ | myFloatingPointNumber: | ||
+ | myVeryBigNumber: | ||
+ | myDate: |2018-12-07|, | ||
+ | myTime: |11:55:56|, | ||
+ | myDateTime: |2018-10-01T23: | ||
+ | myBoolean: true, | ||
+ | myArray: [ 1, 2, 3, 5, 8], | ||
+ | myMixedArray: | ||
+ | myObjectKeyValuePair: | ||
+ | myObjectWithConditionalField: | ||
+ | myNull: null, | ||
+ | myBinary: " | ||
+ | //A one-line comment here. | ||
+ | } | ||
+ | </ | ||
+ | ===== dw::* ===== | ||
==== dw:: | ==== dw:: | ||
Строка 40: | Строка 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 === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import divideBy from dw:: | ||
+ | output application/ | ||
+ | --- | ||
+ | { " | ||
+ | </ | ||
+ | === drop === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import * from dw:: | ||
+ | var samplearray = [" | ||
+ | output application/ | ||
+ | --- | ||
+ | |||
+ | drop(samplearray, | ||
+ | </ | ||
+ | |||
+ | === indexOf === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import * from dw:: | ||
+ | var samplearray = [" | ||
+ | output application/ | ||
+ | --- | ||
+ | |||
+ | indexOf(samplearray, | ||
+ | </ | ||
+ | |||
+ | === join === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import * from dw:: | ||
+ | |||
+ | var items = [{id: " | ||
+ | |||
+ | var Prices = [{Id: " | ||
+ | |||
+ | output application/ | ||
+ | --- | ||
+ | join(items, Prices, (item) -> item.id, (price) -> price.Id) | ||
+ | |||
+ | </ | ||
+ | === takeWhile === | ||
+ | |||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import * from dw:: | ||
+ | output application/ | ||
+ | var arr = [1, | ||
+ | --- | ||
+ | |||
+ | arr takeWhile $ <= 2 | ||
+ | |||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import * from dw:: | ||
+ | output application/ | ||
+ | var obj = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | --- | ||
+ | obj takeWhile ((value, key) -> value < 3) | ||
+ | </ | ||
==== dw::Core ==== | ==== dw::Core ==== | ||
Строка 53: | Строка 376: | ||
--- | --- | ||
payload distinctBy $.id | payload distinctBy $.id | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var str = [1, | ||
+ | var str1= [4,5,6,7,1] | ||
+ | --- | ||
+ | (str ++ str1) distinctBy ((item, index) ->item ) | ||
</ | </ | ||
Строка 320: | Строка 652: | ||
</ | </ | ||
- | === Flatten | + | < |
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload filterObject ((value, key) -> | ||
+ | (key as String != "first name") | ||
+ | and (key as String != "last name" | ||
+ | ) | ||
+ | </ | ||
+ | === flatten | ||
Для перемещения элементов из подмассивов в родительский массив, | Для перемещения элементов из подмассивов в родительский массив, | ||
Строка 438: | Строка 779: | ||
</ | </ | ||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | fun process(obj: | ||
+ | (key): key match { | ||
+ | case " | ||
+ | else -> value | ||
+ | } | ||
+ | }) | ||
+ | --- | ||
+ | payload map ((item, index) -> | ||
+ | process(item) | ||
+ | ) | ||
+ | </ | ||
=== mapObject === | === mapObject === | ||
Строка 593: | Строка 948: | ||
</ | </ | ||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var data = {" | ||
+ | --- | ||
+ | data..value reduce $ | ||
+ | </ | ||
+ | |||
+ | === read === | ||
+ | |||
+ | * **stringToParse** The string or binary to read. | ||
+ | * **contentType** A supported format (or content type). Default: application/ | ||
+ | * **readerProperties** Optional: | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var inputData= | ||
+ | " | ||
+ | Joseph, | ||
+ | James, | ||
+ | --- | ||
+ | read(inputData," | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myVar = "Some, Body" | ||
+ | output application/ | ||
+ | --- | ||
+ | read(myVar," | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | read(' | ||
+ | </ | ||
+ | |||
+ | === readUrl === | ||
+ | |||
+ | * **url** The URL string to read. It also accepts a classpath-based URL. for example: classpath: | ||
+ | * **contentType** A supported format (or MIME type). Default: application/ | ||
+ | * **readerProperties** Optional: | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | readUrl(" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myJsonSnippet = readUrl(" | ||
+ | output application/ | ||
+ | --- | ||
+ | (myJsonSnippet.results map(item) -> item.profile) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | readUrl(" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var inputData=readUrl(" | ||
+ | --- | ||
+ | { | ||
+ | name: inputData.name, | ||
+ | username: inputData.username, | ||
+ | email: inputData.email | ||
+ | } | ||
+ | </ | ||
+ | === splitBy === | ||
+ | |||
+ | Разбивает строку на массив строк на основе входного значения или соответствует части этой входной строки. Он отфильтровывает соответствующую часть из возвращаемого массива. Строка или регулярное выражение Java, используемое для разделения строки. Если какая-то часть строки не соответствует, | ||
+ | |||
+ | Эта версия splitBy принимает регулярное выражение Java (регулярное выражение), | ||
+ | |||
+ | * **text** The input string to split. | ||
+ | * **regex** A Java regular expression used to split the string. If it does not match some part of the string, the function will return the original, unsplit string in the array. | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | { " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | { " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === valuesOf === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | valuesOf(payload.responses) filter ($.fieldname ~= " | ||
+ | </ | ||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload splitBy(" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload splitBy(/ | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | " | ||
+ | </ | ||
==== Working with Objects / dw:: | ==== Working with Objects / dw:: | ||
Строка 607: | Строка 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 === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import everyEntry from dw:: | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | a: {} everyEntry (value, key) -> value is String, | ||
+ | b: {a: "", | ||
+ | c: {a: "", | ||
+ | d: {a: "", | ||
+ | e: {a: "" | ||
+ | f: null everyEntry ((value, key) -> key as String == " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === mergeWith === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import mergeWith from dw:: | ||
+ | output application/ | ||
+ | --- | ||
+ | { " | ||
+ | </ | ||
+ | |||
+ | === someEntry === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | import someEntry from dw:: | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | a: {} someEntry (value, key) -> value is String, | ||
+ | b: {a: "", | ||
+ | c: {a: "", | ||
+ | d: {a: "", | ||
+ | e: {a: "" | ||
+ | f: null someEntry (value, key) -> key as String == " | ||
+ | } | ||
+ | </ | ||
==== dw:: | ==== dw:: | ||
Строка 743: | Строка 1291: | ||
* https:// | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// |