Skip to content

Commit

Permalink
Hedge against the "zero Value"
Browse files Browse the repository at this point in the history
Reflect in Go has the concept of a "zero Value" [1] (not be confused with a
type's zero value with a lowercase "v") and asking for Type on one will
panic. I'm not exactly sure under what conditions these are generated,
but they are occasionally, so here we hedge against them.

See also: #75

Fixes #75.

[1] https://golang.org/pkg/reflect/#Value
  • Loading branch information
brandur committed May 29, 2018
1 parent ad06541 commit 1dbc312
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion generator/datareplacer/datareplacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,18 @@ func ReplaceData(requestData map[string]interface{}, responseData map[string]int
}

func isSameType(v1, v2 interface{}) bool {
return reflect.ValueOf(v1).Type() == reflect.ValueOf(v2).Type()
v1Value := reflect.ValueOf(v1)
v2Value := reflect.ValueOf(v2)

// Reflect in Go has the concept of a "zero Value" (not be confused with a
// type's zero value with a lowercase "v") and asking for Type on one will
// panic. I'm not exactly sure under what conditions these are generated,
// but they are occasionally, so here we hedge against them.
//
// https://github.com/stripe/stripe-mock/issues/75
if !v1Value.IsValid() || !v2Value.IsValid() {
return false
}

return v1Value.Type() == v2Value.Type()
}

0 comments on commit 1dbc312

Please sign in to comment.