instaclustr / operator

Instaclustr Kubernetes Operator
Apache License 2.0
2 stars 0 forks source link

`dcomprasion` StructsDiff was implemented #723

Closed worryg0d closed 8 months ago

worryg0d commented 8 months ago

The PR provides the dcomparison.StructsDiff func which deeply compares two struct and returns dcomparison.ObjectDiffs with differences between both of them.

The path to each field is based on:

  1. json field name if there is a json tag with json field name
  2. the name of the field directly if there is no json field name
  3. if there is a json inline constraint ,inline for an embedded struct it skips its name

The package also provides dcomparisonSkip struct tag which indicates if the struct field should be included in comparasing.

type SomeStruct struct {
       // Field will be included in the result with json field name `field`
    Field string `json:"field"`
        // FieldToBeSkipped will not be included in comparasing
        FieldToBeSkipped `json"fieldToBeSkipped" dcomprasionSkip"true"`
        // unexportedField will not be included too 
        unexportedField `json"unexportedField"`
       // In this case the name FieldWithJSONSkipConstraint will be included in the result 
        FieldWithJSONSkipConstraint `json"-"`
}

type StructWithEmbedded struct {
        // the name of the ExportedStruct will not be included in the result.
        // It means that the resulting path to the field inside 
        // embedded struct will be `StructWithEmbedded.field`
    ExportedStruct `json:",inline"`
}