mennanov / fieldmask-utils

Protobuf Field Mask Go utils
MIT License
229 stars 26 forks source link

feature: src tag #38

Closed zhuliquan closed 10 months ago

zhuliquan commented 10 months ago

some times, mask may using json field. this feature we can copy according to field's tag of src object. useage:

type A struct {
    Field1 string
    Field2 int `db:"some_field"`
}
type B struct {
    Field1 string `struct:"a_name"`
    A      A      `db:"another_name"`
}
src := &B{
    Field1: "B Field1",
    A: A{
        Field1: "A Field 1",
        Field2: 1,
    },
}
mask, _ = fieldmask_utils.MaskFromPaths([]string{"Field1","another_name.some_field"}, func(s string) string {return s})
dst := make(map[string]interface{})
err = fieldmask_utils.StructToMap(mask, src, dst, fieldmask_utils.WithSrcTag("db"))
fmt.Println(err)
// dst is map[string]interface{}{
//  "Field1": src.Field1,
//  "another_name": map[string]interface{}{
//      "some_field": src.A.Field2,
//  },
// }
fmt.Println(dst)
codecov[bot] commented 10 months ago

Codecov Report

Merging #38 (8d44d57) into master (f9573a9) will increase coverage by 0.11%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #38      +/-   ##
==========================================
+ Coverage   86.61%   86.72%   +0.11%     
==========================================
  Files           2        2              
  Lines         478      482       +4     
==========================================
+ Hits          414      418       +4     
  Misses         46       46              
  Partials       18       18              
Files Changed Coverage Δ
copy.go 83.38% <100.00%> (+0.19%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

zhuliquan commented 10 months ago

Thanks for reviewing, I have modified the code according to your suggestions.

mennanov commented 10 months ago

Thanks!