Added diff matcher + fixed remove value struct from collection hashes

This commit is contained in:
Ruben Koster 2019-01-17 13:10:05 +01:00
parent ff7823d5c5
commit fb8dc82190
8 changed files with 88 additions and 27 deletions

1
go.mod
View File

@ -30,6 +30,7 @@ require (
github.com/pivotal-cf/kiln v0.0.0-20181201001855-3e8211a50c91
github.com/pivotal-cf/om v0.0.0-20181130184206-81ba5acde680
github.com/pivotal-cf/pivnet-cli v0.0.55
github.com/sergi/go-diff v1.0.0
github.com/stretchr/testify v1.3.0 // indirect
github.com/ulikunitz/xz v0.5.5 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect

2
go.sum
View File

@ -90,6 +90,8 @@ github.com/pivotal-cf/pivnet-cli v0.0.55/go.mod h1:GIenTUfi8rXtvDXnOuUMfQ+ZofegQ
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robdimsdale/sanitizer v0.0.0-20160522134901-ab2334cb7539/go.mod h1:tqCODtkKV+9Tfvt9JURvKCTxJ69bA/OU/QhsaQLK/rc=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v0.0.0-20180927124308-a11c78ba2c13 h1:hzFIj+Ky1KX599VGAVY//20nam1rYKwQwNVix1sYhXo=
github.com/shirou/gopsutil v0.0.0-20180927124308-a11c78ba2c13/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -1,22 +1,18 @@
package tiles
type properties struct {
// org label
// org description
// default: "system"
Org struct {
Value string `json:"value"`
} `json:".properties.org,omitempty"`
BigtableCustomPlans []struct {
Name struct {
Value string `json:"value"`
} `json:".properties.name"`
// Display Name
// Name of the plan to be displayed to users.
DisplayName struct {
Value string `json:"value"`
} `json:".properties.display_name"`
} `json:".properties.bigtable_custom_plans,omitempty"`
Users struct {
Value []struct {
Name string `json:".name"`
DisplayName string `json:".display_name"`
Password struct {
Secret string `json:"secret"`
} `json:".password"`
} `json:"value"`
} `json:".properties.users,omitempty"`
}

View File

@ -1,12 +1,10 @@
property_blueprints:
- configurable: true
label: org label
description: org description
default: system
name: org
type: string
- configurable: true
name: bigtable_custom_plans
name: users
optional: true
property_blueprints:
- name: guid
@ -18,8 +16,9 @@ property_blueprints:
type: string
unique: true
- configurable: true
description: Name of the plan to be displayed to users.
label: Display Name
name: display_name
type: string
- configurable: true
type: secret
name: password
type: collection

View File

@ -55,17 +55,25 @@ func (r *structRenderer) addFieldsForProperties(properties []proofing.Normalized
}
tag := jsonTag(property.Property, !property.Required || property.Default != nil)
switch property.Type {
case "collection":
if property.Type == "collection" {
cp := r.parser.GetCollectionParser(property)
cr := newStructRenderer(cp)
cr.addFieldsForProperties(cp.AllPropertyBlueprints())
*r.fields = append(*r.fields, Id(propertyToId(property)).
Index().Struct(*cr.fields...).Tag(tag), Line())
default:
*r.fields = append(*r.fields, Id(propertyToId(property)).
Struct(propertyToValueStruct(property)).Tag(tag), Line())
field := Id(propertyToId(property)).Struct(
Id("Value").Index().Struct(*cr.fields...).
Tag(jsonTag("value", false)),
).Tag(tag)
*r.fields = append(*r.fields, field)
continue
}
if r.parser.collection {
*r.fields = append(*r.fields, Id(propertyToId(property)).
Add(propertyToStruct(property)).Tag(tag))
continue
}
*r.fields = append(*r.fields, Id(propertyToId(property)).
Struct(propertyToValueStruct(property)).Tag(tag), Line())
}
}

View File

@ -6,6 +6,7 @@ import (
"runtime"
. "github.com/starkandwayne/pivy/metadata"
. "github.com/starkandwayne/pivy/string_diff_matcher"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -43,7 +44,7 @@ var _ = Describe("GolangRenderer", func() {
It("renders a go file with property struct", func() {
parse(readAsset("property_blueprints.yml"))
Expect(out).To(Equal(standardizeSpaces(readAsset("property_blueprints.go"))))
Expect(out).To(EqualWithDiff(standardizeSpaces(readAsset("property_blueprints.go"))))
})
})
})

View File

@ -13,6 +13,7 @@ import (
type metaDataParser struct {
parsedTemplate proofing.ProductTemplate
rawTemplate *gabs.Container
collection bool
}
func NewParser(metaData []byte) (*metaDataParser, error) {
@ -36,12 +37,22 @@ func NewParser(metaData []byte) (*metaDataParser, error) {
return &metaDataParser{},
fmt.Errorf("second parse metadata failed: %s", err)
}
p.collection = false
return &p, err
}
func (p *metaDataParser) AllPropertyBlueprints() []proofing.NormalizedPropertyBlueprint {
if p.collection {
var propertyBlueprints []proofing.NormalizedPropertyBlueprint
propertyBlueprints = make([]proofing.NormalizedPropertyBlueprint, 0, len(p.parsedTemplate.PropertyBlueprints))
for _, pb := range p.parsedTemplate.PropertyBlueprints {
propertyBlueprints = append(propertyBlueprints, pb.Normalize("")...)
}
return propertyBlueprints
}
return p.parsedTemplate.AllPropertyBlueprints()
}
@ -63,6 +74,7 @@ func (p *metaDataParser) GetCollectionParser(property proofing.NormalizedPropert
if err != nil {
return &metaDataParser{}
}
parser.collection = true
return parser
}

View File

@ -0,0 +1,42 @@
package string_diff_matcher
import (
"fmt"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/matchers"
"github.com/onsi/gomega/types"
"github.com/sergi/go-diff/diffmatchpatch"
)
const defaultStyle = "\x1b[0m"
const redColor = "\x1b[91m"
func EqualWithDiff(expected interface{}) types.GomegaMatcher {
return &equalWithDiffMatcher{
expected: expected,
}
}
type equalWithDiffMatcher struct {
expected interface{}
}
func (matcher *equalWithDiffMatcher) Match(actual interface{}) (success bool, err error) {
em := matchers.EqualMatcher{Expected: matcher.expected}
return em.Match(actual)
}
func (matcher *equalWithDiffMatcher) FailureMessage(actual interface{}) (message string) {
dmp := diffmatchpatch.New()
as, _ := actual.(string)
es, _ := matcher.expected.(string)
diffs := dmp.DiffMain(es, as, true)
return fmt.Sprintf("Expected: \n%s\n%s \n%sto match but found diff: %s\n\n\x1b[0m%s",
defaultStyle, actual, redColor, defaultStyle, dmp.DiffPrettyText(diffs))
}
func (matcher *equalWithDiffMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to equal", matcher.expected)
}