fix: int comes in as float from json

resolves #755
This commit is contained in:
Jan De Dobbeleer 2021-06-23 21:50:05 +02:00 committed by Jan De Dobbeleer
parent 1b0ceaff70
commit fb83354186
3 changed files with 11 additions and 5 deletions

View file

@ -121,12 +121,17 @@ func (p *properties) getInt(property Property, defaultValue int) int {
return defaultValue
}
intValue, ok := val.(int)
if intValue, ok := val.(int); ok {
return intValue
}
// json parses a float
intValue, ok := val.(float64)
if !ok {
return defaultValue
}
return intValue
return int(intValue)
}
func (p *properties) getKeyValueMap(property Property, defaultValue map[string]string) map[string]string {

View file

@ -342,7 +342,7 @@ func (g *git) getGitHEADContext(ref string) string {
func (g *git) truncateBranch(branch string) string {
maxLength := g.props.getInt(BranchMaxLength, 0)
if maxLength == 0 {
if maxLength == 0 || len(branch) < maxLength {
return branch
}
return branch[0:maxLength]

View file

@ -778,9 +778,10 @@ func TestTruncateBranch(t *testing.T) {
MaxLength interface{}
}{
{Case: "No limit", Expected: "all-your-base-are-belong-to-us", Branch: "all-your-base-are-belong-to-us"},
{Case: "No limit - larger", Expected: "all-your-base", Branch: "all-your-base-are-belong-to-us", MaxLength: 13},
{Case: "No limit - smaller", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 13},
{Case: "No limit - larger", Expected: "all-your-base", Branch: "all-your-base-are-belong-to-us", MaxLength: 13.0},
{Case: "No limit - smaller", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 13.0},
{Case: "Invalid setting", Expected: "all-your-base", Branch: "all-your-base", MaxLength: "burp"},
{Case: "Lower than limit", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 20.0},
}
for _, tc := range cases {