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

feat: Draft PR Support Edit Values for JSON operator #1132 #761

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Prototype4988
Copy link

@Prototype4988 Prototype4988 commented Oct 20, 2024

Because

  • Feature for [JSON] Support Edit Values for JSON operator #1132

This commit closes #1132

  • Added task in definiton.json
  • Created Schema for task in tasks.json
  • Implement interface for the task in main.go

@Prototype4988 Prototype4988 changed the title Draft PR Support Edit Values for JSON operator #1132 feat: Draft PR Support Edit Values for JSON operator #1132 Oct 20, 2024
@Prototype4988 Prototype4988 marked this pull request as draft October 20, 2024 17:41
@Prototype4988 Prototype4988 marked this pull request as ready for review October 20, 2024 17:41
@kuroxx kuroxx linked an issue Oct 21, 2024 that may be closed by this pull request
Copy link
Member

@chuang8511 chuang8511 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

Before next code review, could you do a few things for us?

  1. We will assume you have done end-to-end test when your PR is ready. So, please provide your recipe for us.
  2. If you have time, please also add the unit testing code to ensure the functionality.

Thank you again!

@@ -198,5 +198,47 @@
"title": "Output",
"type": "object"
}
},
"TASK_EDIT_VALUES": {
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I found I put the wrong JSON schema in the issue.
Could you fetch this JSON schema?

So, it means your Golang reader will change the key name.
e.g. conflictResolution -> conflict-resolution

@@ -155,3 +162,167 @@ func (e *execution) jq(in *structpb.Struct) (*structpb.Struct, error) {
func (e *execution) Execute(ctx context.Context, jobs []*base.Job) error {
return base.SequentialExecutor(ctx, jobs, e.execute)
}

func (e *execution) updateJson(in *structpb.Struct) (*structpb.Struct, error) {
Copy link
Member

Choose a reason for hiding this comment

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

updateJson

There is another function called TASK_RENAME_FIELDS in the near future.
So, let's make this function name specific. How about just editValue?

data := in.Fields["data"].AsInterface()
updates := in.Fields["updates"].GetListValue().AsSlice()
conflictResolution := in.Fields["conflictResolution"].GetStringValue()
supportDotNotation := in.Fields["supportDotNotation"].GetBoolValue()
Copy link
Member

Choose a reason for hiding this comment

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

In other tasks in JSON, there are only 1 or 2 fields, so they don't use Struct to convert data from schema.

How about we make it as struct? It can convert data easily.
You can take this as sample.

	data := in.Fields["data"].AsInterface()
	updates := in.Fields["updates"].GetListValue().AsSlice()
	conflictResolution := in.Fields["conflictResolution"].GetStringValue()
	supportDotNotation := in.Fields["supportDotNotation"].GetBoolValue()

// Process each object in the array
for i, obj := range data {
if err := applyUpdatesToObject(obj, updates, supportDotNotation, conflictResolution); err != nil {
msg := fmt.Sprintf("Error in object %d: %v\n", i, err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
msg := fmt.Sprintf("Error in object %d: %v\n", i, err)
msg := fmt.Sprintf("apply updates to object %d: %v\n", i, err)

Now, we follow the pattern of describing what it does but not error statement when error handling.

case map[string]interface{}:
// Process the single object
if err := applyUpdatesToObject(data, updates, supportDotNotation, conflictResolution); err != nil {
msg := fmt.Sprintf("Error in single object: %v\n", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
msg := fmt.Sprintf("Error in single object: %v\n", err)
msg := fmt.Sprintf("apply update to single object: %v\n", err)

conflictResolution := in.Fields["conflictResolution"].GetStringValue()
supportDotNotation := in.Fields["supportDotNotation"].GetBoolValue()

// Perform deep copy of the data before updates
Copy link
Member

Choose a reason for hiding this comment

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

I think good comments are for

  • Exported document
  • Reason why the code is like this
  • If it is hard to understand, add what the code does

The code explains itself well, so I'd remove the comments here.
Same as other places.

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

Successfully merging this pull request may close these issues.

[JSON] Support Edit Values for JSON operator
3 participants