Различия
Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
develop:mule:dataweave [2024/02/25 12:14] – mirocow | develop:mule:dataweave [2024/03/06 22:19] (текущий) – [Ссылки] mirocow | ||
---|---|---|---|
Строка 1: | Строка 1: | ||
{{tag> | {{tag> | ||
- | ====== DataWeave | + | ===== DataWeave ===== |
<code dw> | <code dw> | ||
Строка 18: | Строка 18: | ||
</ | </ | ||
- | ===== Working with Objects / dw:: | + | <note tip>IDE https:// |
+ | ===== Oprators | ||
- | * **divideBy** Breaks up an object into sub-objects | + | * using: For initializing local variables in a DataWeave script. |
- | | + | |
- | * **everyEntry** Returns true if every entry in the object matches the condition. | + | |
- | * **keySet** Returns an array of key names from an object. | + | |
- | * **mergeWith** Appends any key-value pairs from a source object | + | |
- | * **nameSet** Returns an array of keys from an object. | + | * not: Logical operator for negation. |
- | * **someEntry** Returns true if at least one entry in the object matches the specified condition. | + | * .@: All attribute selector, for example, in a case that uses the expression payload.root.a.@ to return the attributes and values |
- | * **takeWhile** Selects key-value pairs from the object while the condition | + | ==== Condition ==== |
- | * **valueSet** Returns an array of the values from key-value pairs in an object. | + | |
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myVar = { country : " | ||
+ | output application/ | ||
+ | --- | ||
+ | if (myVar.country == " | ||
+ | | ||
+ | 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 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | === Лямда функции / 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 Equal to | ||
+ | | ||
+ | | ||
+ | * not A Logical negation | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | * ! Negates the result of the input. See also, not. Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later. | ||
+ | | ||
+ | * 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, | ||
+ | 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: | ||
+ | myDateTime: |2018-10-01T23: | ||
+ | myBoolean: true, | ||
+ | myArray: [ 1, 2, 3, 5, 8], | ||
+ | myMixedArray: | ||
+ | myObjectKeyValuePair: | ||
+ | myObjectWithConditionalField: | ||
+ | myNull: null, | ||
+ | myBinary: " | ||
+ | //A one-line comment here. | ||
+ | } | ||
+ | </ | ||
+ | ===== dw::* ===== | ||
- | ===== Working with Arrays / dw:: | + | ==== dw:: |
* **countBy** Counts the elements in an array that return true when the matching function is applied to the value of each element. | * **countBy** Counts the elements in an array that return true when the matching function is applied to the value of each element. | ||
Строка 52: | Строка 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 ==== | ||
+ | |||
+ | === distinctBy === | ||
+ | |||
+ | Выполняет итерацию по массиву и возвращает уникальные элементы в нем. DataWeave использует результат применения предоставленной лямбды в качестве критерия уникальности. Эта версия DifferentBy находит уникальные значения в массиве. Другие версии действуют на объект и обрабатывают нулевое значение. | ||
+ | |||
+ | Эта функция полезна, | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload distinctBy $.id | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var str = [1, | ||
+ | var str1= [4,5,6,7,1] | ||
+ | --- | ||
+ | (str ++ str1) distinctBy ((item, index) ->item ) | ||
+ | </ | ||
+ | |||
+ | === Filter Arrays === | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | id: payload.Id, | ||
+ | markCode: payload.marketCode, | ||
+ | languageCode: | ||
+ | username: payload.profile.base.username, | ||
+ | phoneNumber: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Infix Notation: Long-Hand | ||
+ | < | ||
+ | [ ] filter (value, index) -> (condition) | ||
+ | </ | ||
+ | |||
+ | Infix Notation: $ syntax | ||
+ | < | ||
+ | [ ] filter (< | ||
+ | </ | ||
+ | |||
+ | Prefix Notation | ||
+ | < | ||
+ | filter([ ], (value, index) -> (condition)) | ||
+ | </ | ||
+ | |||
+ | Filter Expression | ||
+ | < | ||
+ | [ ] [? (< | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | [" | ||
+ | filter | ||
+ | (value) -> (value contains " | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | [" | ||
+ | filter | ||
+ | (value, index) -> ((index mod 2) == 0) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | [" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filter | ||
+ | (value, index) -> ((value.cik_str mod 2) == 1) and | ||
+ | (index > 5) and | ||
+ | (value.ticker startsWith | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | [" | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Инфиксная запись / Infix Notation == | ||
+ | |||
+ | Когда вы пишете арифметическое выражение, | ||
+ | |||
+ | До сих пор мы вызывали фильтр, | ||
+ | |||
+ | <note tip> | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | |||
+ | var numbers = (1 to 5) | ||
+ | --- | ||
+ | numbers filter ((n, idx) -> (n mod 2) == 1) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | |||
+ | var numbers = (1 to 5) | ||
+ | --- | ||
+ | numbers | ||
+ | filter ((n, idx) -> (n mod 2) == 1) | ||
+ | filter ((n, idx) -> (n > 3)) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | [ ] filter (value, index) -> (condition) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | [ ] filter (< | ||
+ | </ | ||
+ | |||
+ | == Prefix Notation | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | filter([ ], (value, index) -> (condition)) | ||
+ | </ | ||
+ | === filterObject === | ||
+ | |||
+ | Оператор | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | Infix Notation: Long-Hand | ||
+ | < | ||
+ | { } filterObject (value, | ||
+ | </ | ||
+ | |||
+ | Infix Notation: $ syntax | ||
+ | < | ||
+ | { } filterObject (condition with $, $$ , $$$ syntax) | ||
+ | </ | ||
+ | |||
+ | Prefix Notation | ||
+ | < | ||
+ | filterObject({ }, (value, index) -> (condition)) | ||
+ | </ | ||
+ | |||
+ | Filter Expression | ||
+ | < | ||
+ | { } [? (< | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload filterObject (value, key, index) -> key == " | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload filterObject (value, key, index) -> key~=" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var myObject = { | ||
+ | str1 : " | ||
+ | str2 : " | ||
+ | str3 : null, | ||
+ | str4 : " | ||
+ | } | ||
+ | --- | ||
+ | myObject filterObject $ != null | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filterObject | ||
+ | (value, key, index) -> ((value.ticker contains " | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | Filter object by key | ||
+ | |||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filterObject | ||
+ | (value, key, index) -> (((key as Number) mod 2) == 0) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | Filter object by index | ||
+ | |||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filterObject | ||
+ | (value, key, index) -> (((index) mod 2) == 0) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | Filter object by value with $ syntax | ||
+ | |||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filterObject | ||
+ | $ is Object | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filterObject | ||
+ | $ is Array | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | Filter object by key with $$ syntax | ||
+ | |||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filterObject | ||
+ | ((($$ as Number) mod 2) == 0) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | Filter object by index with $$$ syntax | ||
+ | |||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload | ||
+ | filterObject | ||
+ | (($$$ mod 2) == 0) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | Filter object using the filter expression selector | ||
+ | |||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | |||
+ | --- | ||
+ | payload [?( $.ticker contains " | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload filterObject ((value, key) -> | ||
+ | (key as String != "first name" | ||
+ | and (key as String != "last name") | ||
+ | ) | ||
+ | </ | ||
+ | === flatten === | ||
+ | |||
+ | Для перемещения элементов из подмассивов в родительский массив, | ||
+ | |||
+ | * **items** The input array of arrays made up of any supported types. | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | flatten(payload) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var array1 = [1,2,3] | ||
+ | var array2 = [4,5,6] | ||
+ | var array3 = [7,8,9] | ||
+ | var arrayOfArrays = [array1, array2, array3] | ||
+ | --- | ||
+ | flatten(arrayOfArrays) | ||
+ | </ | ||
+ | === groupBy === | ||
+ | |||
+ | Функция groupBy принимает список объектов [x1,…,xn] и некоторую функцию f. Затем она отображает f в списке, | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload groupBy (n, idx) -> isEven(n) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload groupBy (n, idx) -> n.dayOfWeek | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var user = {" | ||
+ | --- | ||
+ | user groupBy ((value, key) -> if (key contains ' | ||
+ | </ | ||
+ | |||
+ | === map === | ||
+ | |||
+ | Функция карты используется для преобразования данных, | ||
+ | |||
+ | Перебирает элементы массива и выводит результаты в новый массив. | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | }) | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload map (n, idx) -> n + 1 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload.items.*name map ( | ||
+ | (item, index) -> {name: item} | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | %dw 2.0 | ||
+ | var myVar = [ | ||
+ | { bookId: 101, | ||
+ | title: "world history", | ||
+ | price: " | ||
+ | }, | ||
+ | { | ||
+ | bookId: 202, | ||
+ | title: 'the great outdoors', | ||
+ | price: " | ||
+ | } | ||
+ | ] | ||
+ | var myVar2 = [ | ||
+ | { | ||
+ | bookId: 101, | ||
+ | author: "john doe" | ||
+ | }, | ||
+ | { | ||
+ | bookId: 202, | ||
+ | author: "jane doe" | ||
+ | } | ||
+ | ] | ||
+ | output application/ | ||
+ | --- | ||
+ | myVar map (item, index) -> using (id = item.bookId) { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | (myVar2 filter ($.*bookId contains id) map (item) -> { | ||
+ | author : item.author | ||
+ | }) | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | fun process(obj: | ||
+ | (key): key match { | ||
+ | case " | ||
+ | else -> value | ||
+ | } | ||
+ | }) | ||
+ | --- | ||
+ | payload map ((item, index) -> | ||
+ | process(item) | ||
+ | ) | ||
+ | </ | ||
+ | === mapObject === | ||
+ | |||
+ | Функция | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | {" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload mapObject (value, key, index) -> { | ||
+ | (upper(key)): | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var user = {" | ||
+ | var userList = [{" | ||
+ | {" | ||
+ | ] | ||
+ | --- | ||
+ | { | ||
+ | mapObject: user mapObject (value: | ||
+ | ' | ||
+ | }, | ||
+ | |||
+ | pluck: user pluck (value, key, index) -> (key ++ ' | ||
+ | |||
+ | filter: user filterObject ((value, key, index) -> (index < 1)), | ||
+ | filter2: userList filter ((value, index) -> (index < 1)), | ||
+ | |||
+ | groupBy1 : user groupBy (value, key) -> key endsWith ' | ||
+ | groupBy2 : userList groupBy ((value, index) -> value.lastName) | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | === pluck === | ||
+ | |||
+ | это функция, | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload pluck (v,k,idx) -> {(k): v} | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var requestBody= | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | --- | ||
+ | { | ||
+ | value: requestBody pluck $, | ||
+ | keys: requestBody pluck $$, | ||
+ | indexes: requestBody pluck $$$ | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | var requestBody= | ||
+ | { | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | --- | ||
+ | car: | ||
+ | </ | ||
+ | === reduce === | ||
+ | |||
+ | Функция уменьшения используется для выполнения любых вычислений во время итерации массива, | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output json | ||
+ | --- | ||
+ | payload reduce (n, total) -> total + n | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | [2, 3] reduce ($ + $$) | ||
+ | |||
+ | Here | ||
+ | |||
+ | [2,3] – is the input array | ||
+ | acc -> $$ will take the 1st item value = 2 (as acc is not initialized) | ||
+ | $ will take the 2nd item value = 3 (as acc is not initialized) | ||
+ | Loop count = no of item in array minus 1 (as acc is not initialized) = 2 – 1 = 1 | ||
+ | Acc = ($=3 + $$=2) = 5 | ||
+ | |||
+ | So result will be 5 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | [2,3] reduce ((item, acc = 4) -> acc + item) | ||
+ | |||
+ | Here | ||
+ | |||
+ | [2,3] – is the input array | ||
+ | acc will take the initialized value = 4 | ||
+ | item will take 1st item value = 2 (as acc is initialized) | ||
+ | Loop count = no of item in array (as acc is initialized) = 2 | ||
+ | Acc = acc + item -> 4 + 2 -> 6 | ||
+ | Acc = acc + item -> 6 + 3 -> 9 | ||
+ | |||
+ | So result will be 9 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %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:: | ||
+ | |||
+ | * **divideBy** Breaks up an object into sub-objects that contain the specified number of key-value pairs. | ||
+ | * **entrySet** Returns an array of key-value pairs that describe the key, value, and any attributes in the input object. | ||
+ | * **everyEntry** Returns true if every entry in the object matches the condition. | ||
+ | * **keySet** Returns an array of key names from an object. | ||
+ | * **mergeWith** Appends any key-value pairs from a source object to a target object. | ||
+ | * **nameSet** Returns an array of keys from an object. | ||
+ | * **someEntry** Returns true if at least one entry in the object matches the specified condition. | ||
+ | * **takeWhile** Selects key-value pairs from the object while the condition is met. | ||
+ | * **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:: | ||
+ | |||
+ | === Selector Expressions (update Operator) === | ||
+ | |||
+ | update оператор позволяет обновлять указанные поля структуры данных новыми значениями. | ||
+ | Этот селектор соответствия может проверять совпадения с любым значением внутри объекта. | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myInput = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | output application/ | ||
+ | --- | ||
+ | myInput update { | ||
+ | case age at .age -> age + 1 | ||
+ | case s at .address.street -> "First Street" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload update { | ||
+ | case s at .addresses[0] -> { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload update { | ||
+ | case name at .user.@name -> upper(name) | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var theFieldName = " | ||
+ | output application/ | ||
+ | --- | ||
+ | payload update { | ||
+ | case s at ." | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload map ((user) -> | ||
+ | user update { | ||
+ | case name at .name if(name == " | ||
+ | case name at .name if(name == " | ||
+ | } | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myInput = [{" | ||
+ | output application/ | ||
+ | --- | ||
+ | myInput | ||
+ | value update { | ||
+ | case name at .name! -> if(name == null) " | ||
+ | } | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload update { | ||
+ | // | ||
+ | //case age at .age -> age + 1 | ||
+ | case .age -> $ + 1 | ||
+ | case .address.street -> "First Street" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myInput = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | output application/ | ||
+ | --- | ||
+ | myInput mapObject ((value, | ||
+ | if(key as String == " | ||
+ | | ||
+ | else | ||
+ | | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | var myInput = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | output application/ | ||
+ | --- | ||
+ | myInput update { | ||
+ | case age at .age -> age + 1 | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | %dw 2.0 | ||
+ | output application/ | ||
+ | --- | ||
+ | payload map ((item) -> | ||
+ | item.ServiceDetails update { | ||
+ | case Designation at .Designation if (Designation == " | ||
+ | }) | ||
+ | </ | ||
====== Ссылки ====== | ====== Ссылки ====== | ||
* https:// | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// |