Add DataWeave Library Extension & ublish DW Library Project to Exchange

Add DataWeave Library Extension & ublish DW Library Project to Exchange

DataWeave:

DataWeave is?a programming language designed by MuleSoft for accessing and transforming data that travels through a Mule application

DataWeave Playground:

The DataWeave Playground?enables developers to create mock data transformations in their web browser based on an input payload

Why we need to use DataWeave Library Extension:

The DataWeave extension adds language support that enables you to develop, debug, test DataWeave scripts and quickly start creating DataWeave libraries.

Requirements:

  1. Java 8 or 11 provided
  2. ?Apache Maven
  3. ?Microsoft Visual Studio Code for your specific operating system
  4. ?DataWeave 2.0 (BETA) library in VS code (Extensions tab)

How to add Dataweave Extenson:

  1. Open Visual Studio Code, then click on the extension button on the left side i.e pic – 1

No alt text provided for this image

  1. Search for DataWeave and click install i.e pic – 2

No alt text provided for this image

Why we need libraries ?

To reuse the Dataweave modules over and over in same/different projects, Dataweave libraries is a set of or particular functionality in a module, to create your own custom dataweave library follow below steps.

Create a DataWeave Project:

  1. ?Open VSCode.
  2. Click on view -> command palette.
  3. ?Search with dataweave.
  4. ?Select Dataweave: Create New Library Project.
  5. Config below values with respective details.

  • Organization ID -> fill with your anypoint platform account Organization ID
  • Artifact ID -> (your choice)
  • Version -> 1.0.0-SNAPSHOT
  • Project name -> formatData

6. Choose a directory folder for your new project and Select ok

How to create new dataweave module:

  1. Click on view -> command palette.
  2. Search with dataweave.
  3. Select Dataweave: Create New module.
  4. Add the name and press enter – new .dwl file creates under src/main/dw
  5. Add the below code i.e pic- 3 or create your own function.
  6. Once 5th point done, In the same module file, there is option to create unit test and generate dataweave documentation(if you use below code the go with unit test if not generate dataweave documentation and unit test).
  7. When you click create unit test, it will create a Module test file. You can modify the description and input payload to test the module dataweave transformation (if you use below code the use below test code under unit test).

/*

* This module will be shared through your library, feel free to modify it as you please.

*

* You can try it out with the mapping on the src/test/dw directory.

*/

%dw 2.0

?

//change the key/value to upper case condition to change key then inType is 'upperkey' if change value then inType is 'uppervalue'

fun upperData(inData,inType,from,to) = (

??? inData mapObject ((value, key, index) -> {

??????? (if(lower(inType) ~= "upperkey")upper(key) else key) : (

??????????? if(value is Array)keyformatArray(value,inType,from,to)

??????????? else if(value is Object)upperData(value,inType,from,to)

??????????? else if(lower(inType) ~= "uppervalue")upper(value) else value)

???????????

??????????? }))

?

//change the key/value to lower case condition to change key then inType is 'lowerkey' if change value then inType is 'lowervalue'

fun lowerData(inData,inType,from,to) = (

??? inData mapObject ((value, key, index) -> {

??????? (if(lower(inType) ~= "lowerkey")lower(key) else key) : (

???????? ???if(value is Array)keyformatArray(value,inType,from,to)

??????????? else if(value is Object)lowerData(value,inType,from,to)

??????????? else if(lower(inType) ~= "lowervalue")lower(value) else value)

???????????

??????????? }))

?

//change the value from value to value then pass 3rd and 4th parameter in formatData function 3rd postion indicates from value 4th parameter indicates to value

fun change(inData,inType,from,to) = (

??? inData mapObject ((value, key, index) -> {

??????? (key) : (

??????????? if(value is Array)keyformatArray(value,inType,from,to)

??????????? else if(value is Object)change(value,inType,from,to)

??????????? else value replace (from default "") with (to default "")

??????????? )

??????????? }))

?

fun keyformatArray(inData,inType,from,to) = (

??? inData map ((item, index) ->formatData(item,inType,from,to))

??? )

/**

* Describes the `formatData` function purpose.

*

* === Parameters

*

* [%header, cols="1,1,3"]

* |===

* | Name | Type | Description

* | `inData` | Object or Array of Objects |

* | `inType` | String | it work based on upperkey or uppervalue or lowerkey or lowervalue or change

* | `from` | String or null | if inType is change need to pass this value otherwise null

* | `to` | String or null | if inType is change need to pass this value otherwise null

* |===

*

* === Example

*

* This example shows how the `formatData` function behaves under different inputs.

*

* ==== Source

*

* [source,DataWeave,linenums]

* ----

* %dw 2.0

* output application/json

*

* import * from formatData

* var indata = {

*?? "ResourceType": "Immunization",

*? "id": "M197197986",

*? "meTa": {

*??? "versionId": "1K0",

*??? "lastUpdated": "2022-05-05T08:05:34+00:00"

*? }

* }

* ---

* formatData(indata,"upperKey",null,null)

*

* ----

*

* ==== Output

*

* {

* "RESOURCETYPE": "Immunization",

* "ID": "M197197986",

* "META": {

*?? "VERSIONID": "1K0",

*?? "LASTUPDATED": "2022-05-05T08:05:34+00:00"

*? }

* }

* ----

*

* ----

*

*/

fun formatData(inData,inType,from,to) =(

??? if(inData is Array)keyformatArray(inData,inType,from,to)

??? else if(inData is Object)(

??????? if(lower(inType) contains "upper") upperData(inData,inType,from,to)

??????? else if(lower(inType) contains "lower") lowerData(inData,inType,from,to)

??????? else if( inType contains "change") change(inData,inType,from,to)

??????? else

??????? inData

??? )

??? else inData

??? )

?*        

Unit Test:-

No alt text provided for this image

Test Functionality:-

No alt text provided for this image
No alt text provided for this image

8.?Update the Org Id: Change the groupId to org id of anypoint account in pom.xml file. Uncomment the repository and distribution management in pom.xml file. Also replace these ORGANIZATION_ID to org id of anypoint account

No alt text provided for this image

9.?Maven Settings: Create a maven settings.xml file in root folder and update the credentials of anypoint platform in settings.xml to publish the dataweave library. Both repository id and server id should be same.

<servers>
?? <server>
??????? <id>exchange</id>
??????? <username>anypoint username</username>
??????? <password>anypoint password</password>
??? </server>
?</servers>        

10. How to publish dataweave module

Go to terminal and run below cmd

mvn deploy -s maven-settings.xml        

11. verify the dataweave library in exchange by login into configured(setting.xml) platform account

No alt text provided for this image

12. To use published dataweave module in your project, in exchange open recently created library and go to dependency snippets, and copy the maven dependency.

13. Add the dependency in you project pom file.

No alt text provided for this image


要查看或添加评论,请登录

Kancharla Sandeep Sai Kumar的更多文章

社区洞察

其他会员也浏览了