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

Improve the JSON output format for error fields in the fromBytes() API of the copybook #6990

Open
MohamedSabthar opened this issue Sep 12, 2024 · 0 comments

Comments

@MohamedSabthar
Copy link
Member

Description:
Currently, if there are any partial errors while parsing the copybook, they are added to the payload in the following format:

{
  "data": {
    "root": {
      "child": {
        "leaf1": "data",
        "leaf2": null // This is null due to errors
      }
    },
    "errors": [
      "Failed to convert value 'ABC' to decimal at '$.root.child.leaf2'."
    ]
  }
}

Instead of returning errors as shown above, we could improve the error portion of the payload and return a payload like this:

{
  "data": {
    "root": {
      "child": {
        "leaf1": "data",
        "leaf2": null // This is null due to errors
      }
    },
    "errors": {
      "root": {
        "child": {
          "leaf2": "Failed to convert value 'ABC' to decimal."
        }
      }
    }
  }
}

This would allow the user to easily obtain the errors of specific fields without doing much manual work.

Describe your solution(s)
following sample algorithm will map the current output the described format.

import ballerina/io;

public function main() {
    string[] errorPaths = ["$.root.child1.leaf1", "$.root.child1.leaf2", "$.root.leaf3"];
    string[] errorVals = ["failed leaf 1", "failed leaf 2", "failed leaf 3"];

    map<json> errors = {};
    json parent = errors;
    foreach int i in 0 ..< errorPaths.length() {
        string[] pathFragments = re `\.`.split(errorPaths[i]);
        while pathFragments.length() != 0 {
            string fragment = pathFragments.shift();
            if fragment == "$" {
                parent = errors;
                continue;
            }
            if parent is map<json> && !parent.hasKey(fragment) {
                parent[fragment] = pathFragments.length() == 0 ? errorVals[i] : {};
                parent = parent.get(fragment);
            } else if parent is map<json> {
                parent = parent.get(fragment);
            }
        }
    }
    io:println(errors);
}

Related Issues (optional):

Suggested Labels (optional):

Suggested Assignees (optional):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: BackLog
Development

No branches or pull requests

1 participant