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

Support filter and prune on repeated fields #7

Open
alok87 opened this issue Sep 16, 2022 · 1 comment
Open

Support filter and prune on repeated fields #7

alok87 opened this issue Sep 16, 2022 · 1 comment
Assignees

Comments

@alok87
Copy link

alok87 commented Sep 16, 2022

fmutils.Prune(&currentColor, []string{"colors[1]")

Proto

message color {
  string name = 1;
}

message mycolors {
   repeated color colors = 1;
}

Go Types for Proto

type Color struct {
  name string
}

type MyColors {
  colors []Colors
}

Feature request

import "github.com/meannanov/fmutils"


func PatchList(currentColor MyColor, newColor MyColor, paths []string) (&MyColor, error) {
	fieldMask := &field_mask.FieldMask{
		Paths: paths,
	}
	fieldMask.Normalize()

	if !fieldMask.IsValid(&currentColor) {
		return  nil, fmt.Errorf("invalid fields passed in patch:%+v", paths)
	}

	// Redact the newColor to keep only the fields provided for patch
	fmutils.Filter(&newColor, fieldMask.GetPaths())

	// Need to prune the currenColor for not affecting the non mentioned fields in field-mask during merge
	fmutils.Prune(&currentColor, fieldMask.GetPaths())

	// merges the existing object to have the patched fields' data
	proto.Merge(&currentColor, &newColor)
	
	return &currentColor, nil
}

func main() {
   myColors := MyColors{
     colors: []Color{"blue", "green", "white"},
   }
  
   // want to patch 1st index of newColor to myColor
   patchedColor := PatchList(myColor, newColor, []string{"colors[1]"})

}

Protobuf at present does not support it protocolbuffers/protobuf#8547 but this would be really helpful

@mennanov mennanov self-assigned this Feb 8, 2023
@mennanov
Copy link
Owner

@alok87 Please, checkout #10 and see whether it helps getting closer to what is requested in this feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants