Skip to content

Commit

Permalink
fix: int comes in as float from json
Browse files Browse the repository at this point in the history
resolves #755
  • Loading branch information
JanDeDobbeleer committed Jun 23, 2021
1 parent 1b0ceaf commit a48e0b1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/segment_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions src/segment_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a48e0b1

Please sign in to comment.