Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk): making the curate v2 mappings work #1619

Merged
merged 8 commits into from
Jun 25, 2024

Conversation

kemuru
Copy link
Contributor

@kemuru kemuru commented Jun 17, 2024

ready for review.

LATEST UPDATE:
I updated replacePlaceholdersWithValues() to use mustache instead of the direct string manipulation it was doing. I updated the Escrow V2 and Curate V2 mappings to add triple curly braches in some places too, you can find these updated mappings in the links below. contract would need to be updated with the updated templates/mappings too.

This PR makes the curate v2 mappings work. Also allows accessing indexes of an array returned by the subgraph.

How to check that the mappings work:

  1. go to https://deploy-preview-1619--kleros-v2.netlify.app/#/dispute-template
  2. paste these 3 things: externalDisputeID, template, and data mappings. You will find them below:

Curate V2 template and mappings: kleros/curate-v2#33 (comment)
Escrow V2 template and mappings: kleros/escrow-v2#12 (comment)

The idea is that, with just an externalDisputeID, we are able to populate all the needed information from any arbitrable, such as escrow v2, curate v2, or any arbitrable that we integrate with. that would be ideal and would make integrations super easy

We should make this SDK super bullet proof and understandable, make it a good dev experience, not only for the projects we integrate with, but also for us while building this.

(OUTDATED...
image
)

(OUTDATED.... on the SDK, I reverted some of the changes to jsonAction() and replacePlaceholdersWithValues(). basically Aleix reviewed and I realized that my changes are a hacky way to make the curate v2 "json" mapping work, but it will break normal jsons.
context: we upload the item data json and registry metadata json with a different format to the contracts (picture 2) (subgraph link here with the query) and our jsonAction() mapping can't parse that. possible solutions?:
upload the item data json and registry metadata jsons, with the supported format directly, or,
make the jsonAction() mapping more sophisticated and handle the both cases (normal jsons, and stringified jsons with backlashes "") correctly)

image (35)

image (36)

IGNORE:

some JSON Mappings I need to paste here to not lose this info, this is for testing purposes, ignore:

template:

{
    "title": "A reality.eth question",
    "description": "A reality.eth question has been raised to arbitration.",
    "question": "{{ question }}",
    "type": "{{ type }}",
    "answers": [
        "{{# answers }}",
        {
            "title": "{{ title }}",
            "description": "{{ description }}",
            "id": "{{ id }}",
            "reserved": "{{ reserved }}"
        },"{{^ last }}","{{/ last }}",
        "{{/ answers }}"
        
    ],
    "policyURI": "/ipfs/QmZ5XaV2RVgBADq5qMpbuEwgCuPZdRgCeu8rhGtJWLV6yz",
    "frontendUrl": "https://reality.eth.limo/app/#!/question/{{ realityAddress }}-{{ questionId }}",
    "arbitrableChainID": "100",
    "arbitrableAddress": "0x2e39b8f43d0870ba896f516f78f57cde773cf805",
    "arbitratorChainID": "421613",
    "arbitratorAddress": "0xD08Ab99480d02bf9C092828043f611BcDFEA917b",
    "category": "Oracle",
    "lang": "en_US",
    "specification": "KIP99",
    "version": "1.0"
}

data mappings:

[
  {
    "type": "json",
    "value": {
      "source": "**Kleros Moderate:** Did the user, **degenape6** (ID: 1554345080), break the Telegram group, ***[Kleros Trading Group]()*** (ID: -1001151472172), ***[rules](https://cdn.kleros.link/ipfs/Qme3Qbj9rKUNHUe9vj9rqCLnTVUCWKy2YfveQF8HiuWQSu/Kleros%20Moderate%20Community%20Rules.pdf)*** due to conduct related to the ***[message](https://t.me/c/1151472172/116662)*** (***[backup](https://cdn.kleros.link/ipfs/QmVbFrZR1bcyQzZjvLyXwL9ekDxrqHERykdreRxXrw4nqg/animations_file_23.mp4)***)?",
      "type": "single-select",
      "answers": [
        {
          "title": "Refuse to Arbitrate or Invalid",
          "id": "0x00",
          "reserved": true
        }
      ],
      "question": "0xe2a3bd38e3ad4e22336ac35b221bbbdd808d716209f84014c7bc3bf62f8e3b39",
      "realityAddress": "0x14a6748192aBC6E10CA694Ae07bDd4327D6c7A51"
    },
    "seek": [
      "source",
      "type",
      "answers",
      "realityAddress"
    ],
    "populate": [
      "question",
      "type",
      "answers",
      "realityAddress"
    ]
  }
]

PR-Codex overview

The focus of this PR is to improve data mapping functionality in the kleros-sdk by enhancing JSON parsing and placeholder replacement.

Detailed summary

  • Improved JSON parsing in jsonAction.ts
  • Implemented placeholder replacement using mustache in replacePlaceholdersWithValues.ts
  • Enhanced nested value retrieval in createResultObject.ts

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Summary by CodeRabbit

  • New Features

    • Enhanced placeholder replacement logic using the mustache library, providing more robust and flexible string handling.
  • Bug Fixes

    • Improved JSON parsing before creating result objects, ensuring consistent handling of string and object inputs.
    • Enhanced nested property access in result object creation for more accurate data mapping.

These changes improve the overall functionality and reliability of the data mapping processes.

Copy link
Contributor

coderabbitai bot commented Jun 17, 2024

Walkthrough

The recent updates mainly enhance the kleros-sdk by introducing JSON parsing, refining object property handling, and improving string placeholder replacements. Key changes include JSON parsing in jsonAction function, the addition of a nested property access helper in createResultObject, and enhanced replacePlaceholdersWithValues functionality using the mustache library.

Changes

File Path Change Summary
.../jsonAction.ts Added JSON parsing for value before processing.
.../executeActions.ts Introduced a blank line after the await executeAction call, no functional changes.
.../createResultObject.ts Changed seek and populate to arrays, added nested property access helper (getNestedValue).
.../replacePlaceholdersWithValues.ts Improved placeholder replacements using mustache, added logic for string pattern handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SDK as kleros-sdk
    participant JsonAction as jsonAction
    participant ResultObj as createResultObject

    User->>SDK: Call jsonAction with mapping
    SDK->>JsonAction: jsonAction(mapping)
    JsonAction->>JsonAction: Parse value if string
    JsonAction-->>ResultObj: createResultObject(parsedValue, seek, populate)
    ResultObj->>JsonAction: Return result
    JsonAction->>SDK: Return result
    SDK->>User: Provide processed result
Loading

Poem

In the land of code so bright,
Where JSON strings take flight,
Nested keys with glee,
Unlock truths like a key,
Through mustache and seek,
Data's future, sleek,
For devs, a wondrous sight!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Jun 17, 2024

Deploy Preview for kleros-v2-university failed. Why did it fail? →

Name Link
🔨 Latest commit 1620fda
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-university/deploys/6670c7c2644c690008f417f4

Copy link

netlify bot commented Jun 17, 2024

Deploy Preview for kleros-v2 ready!

Name Link
🔨 Latest commit 1620fda
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2/deploys/6670c7c2ec8ef10008cbc1fa
😎 Deploy Preview https://deploy-preview-1619--kleros-v2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jun 17, 2024

Deploy Preview for kleros-v2-neo ready!

Name Link
🔨 Latest commit 1620fda
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-neo/deploys/6670c7c20406550008d8bf7c
😎 Deploy Preview https://deploy-preview-1619--kleros-v2-neo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jun 17, 2024

Deploy Preview for kleros-v2-university failed. Why did it fail? →

Name Link
🔨 Latest commit 78dbbf6
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-university/deploys/667a20f56fc82300082768f1

Copy link

netlify bot commented Jun 17, 2024

Deploy Preview for kleros-v2-neo ready!

Name Link
🔨 Latest commit 78dbbf6
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-neo/deploys/667a20f585c1660008900f28
😎 Deploy Preview https://deploy-preview-1619--kleros-v2-neo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jun 17, 2024

Deploy Preview for kleros-v2 ready!

Name Link
🔨 Latest commit 78dbbf6
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2/deploys/667a20f5c147fc0008436eb9
😎 Deploy Preview https://deploy-preview-1619--kleros-v2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@kemuru kemuru marked this pull request as ready for review June 18, 2024 17:40
@kemuru kemuru changed the title chore(sdk): trying to make the curate v2 mappings work, adding logs chore(sdk): making the curate v2 mappings work Jun 18, 2024
@kemuru kemuru requested a review from alcercu June 19, 2024 16:00
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ee63a5d and 99bafd7.

Files selected for processing (4)
  • kleros-sdk/src/dataMappings/actions/jsonAction.ts (1 hunks)
  • kleros-sdk/src/dataMappings/executeActions.ts (1 hunks)
  • kleros-sdk/src/dataMappings/utils/createResultObject.ts (1 hunks)
  • kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • kleros-sdk/src/dataMappings/executeActions.ts
Additional context used
GitHub Check: CodeQL
kleros-sdk/src/dataMappings/utils/createResultObject.ts

[failure] 8-8: Incomplete string escaping or encoding
This replaces only the first occurrence of "]".

Biome
kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts

[error] 5-5: Do not access Object.prototype method 'hasOwnProperty' from target object. (lint/suspicious/noPrototypeBuiltins)

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.


[error] 10-12: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 13-15: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.

Additional comments not posted (1)
kleros-sdk/src/dataMappings/utils/createResultObject.ts (1)

2-21: The changes to accept arrays for seek and populate enhance the function's flexibility in handling data mappings. The addition of getNestedValue is a solid improvement for accessing nested properties.

Tools
GitHub Check: CodeQL

[failure] 8-8: Incomplete string escaping or encoding
This replaces only the first occurrence of "]".

@kemuru kemuru marked this pull request as draft June 20, 2024 16:21
@kemuru kemuru marked this pull request as ready for review June 21, 2024 00:26
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts (1)

Line range hint 5-5: Replace hasOwnProperty with Object.hasOwn for better compliance and safety.

The use of hasOwnProperty directly on objects is generally discouraged due to potential issues with prototype chain interference. Using Object.hasOwn is safer and more modern.

- if (context.hasOwnProperty(variableName)) {
+ if (Object.hasOwn(context, variableName)) {
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 99bafd7 and 3ad99b1.

Files selected for processing (1)
  • kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts (1 hunks)
Additional context used
Biome
kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts

[error] 5-5: Do not access Object.prototype method 'hasOwnProperty' from target object. (lint/suspicious/noPrototypeBuiltins)

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.


[error] 7-9: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.

Additional comments not posted (1)
kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts (1)

12-13: Parsing logic for nested JSON strings is correctly implemented.

The use of regex to replace nested JSON strings with actual JSON objects before parsing is a clever way to handle cases where JSON strings are embedded within JSON strings.

@kemuru kemuru requested a review from jaybuidl June 21, 2024 00:30
@kemuru kemuru marked this pull request as draft June 21, 2024 09:16
@kemuru kemuru marked this pull request as ready for review June 21, 2024 09:52
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3ad99b1 and f6f1e4e.

Files selected for processing (3)
  • kleros-sdk/src/dataMappings/actions/jsonAction.ts (1 hunks)
  • kleros-sdk/src/dataMappings/utils/createResultObject.ts (1 hunks)
  • kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • kleros-sdk/src/dataMappings/actions/jsonAction.ts
Additional context used
Biome
kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts

[error] 7-13: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 9-13: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)


[error] 11-13: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

GitHub Check: CodeQL
kleros-sdk/src/dataMappings/utils/createResultObject.ts

[failure] 7-7: Incomplete string escaping or encoding
This replaces only the first occurrence of "]".

alcercu
alcercu previously approved these changes Jun 24, 2024
Copy link
Contributor

@alcercu alcercu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@alcercu alcercu enabled auto-merge June 24, 2024 10:59
Copy link

codeclimate bot commented Jun 25, 2024

Code Climate has analyzed commit 78dbbf6 and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2

View more on Code Climate.

Copy link

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f6f1e4e and 78dbbf6.

Files selected for processing (1)
  • kleros-sdk/src/dataMappings/utils/createResultObject.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • kleros-sdk/src/dataMappings/utils/createResultObject.ts

@alcercu alcercu added this pull request to the merge queue Jun 25, 2024
Merged via the queue into dev with commit 6ac5035 Jun 25, 2024
17 of 23 checks passed
@kemuru kemuru deleted the feat(sdk)/improve-mapping-json-action branch June 25, 2024 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants