-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
648864e
commit ba9ed71
Showing
14 changed files
with
157 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package mars | ||
|
||
import ( | ||
"net/url" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
cookieKeyValueParser = regexp.MustCompile("\x00([^:]*):([^\x00]*)\x00") | ||
) | ||
|
||
// parseKeyValueCookie takes the raw (escaped) cookie value and parses out key values. | ||
func parseKeyValueCookie(val string, cb func(key, val string)) { | ||
val, _ = url.QueryUnescape(val) | ||
if matches := cookieKeyValueParser.FindAllStringSubmatch(val, -1); matches != nil { | ||
for _, match := range matches { | ||
cb(match[1], match[2]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mars | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// Return the reflect.Method, given a Receiver type and Func value. | ||
func findMethod(recvType reflect.Type, funcVal reflect.Value) *reflect.Method { | ||
// It is not possible to get the name of the method from the Func. | ||
// Instead, compare it to each method of the Controller. | ||
for i := 0; i < recvType.NumMethod(); i++ { | ||
method := recvType.Method(i) | ||
if method.Func.Pointer() == funcVal.Pointer() { | ||
return &method | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mars | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type T struct{} | ||
|
||
func (t *T) Hello() {} | ||
|
||
func TestFindMethod(t *testing.T) { | ||
for name, tv := range map[string]struct { | ||
reflect.Type | ||
reflect.Value | ||
}{ | ||
"Hello": {reflect.TypeOf(&T{}), reflect.ValueOf((*T).Hello)}, | ||
"Helper": {reflect.TypeOf(t), reflect.ValueOf((*testing.T).Helper)}, | ||
"": {reflect.TypeOf(t), reflect.ValueOf((reflect.Type).Comparable)}, | ||
} { | ||
m := findMethod(tv.Type, tv.Value) | ||
if name == "" { | ||
if m != nil { | ||
t.Errorf("method found that shouldn't be here: %v", m) | ||
} | ||
continue | ||
} | ||
if m == nil { | ||
t.Errorf("No method found when looking for %s", name) | ||
continue | ||
} | ||
if m.Name != name { | ||
t.Errorf("Expected method %s, got %s: %v", name, m.Name, m) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package testing | ||
|
||
import "reflect" | ||
|
||
// Equal is a helper for comparing value equality, following these rules: | ||
// - Values with equivalent types are compared with reflect.DeepEqual | ||
// - int, uint, and float values are compared without regard to the type width. | ||
// for example, Equal(int32(5), int64(5)) == true | ||
// - strings and byte slices are converted to strings before comparison. | ||
// - else, return false. | ||
func Equal(a, b interface{}) bool { | ||
if reflect.TypeOf(a) == reflect.TypeOf(b) { | ||
return reflect.DeepEqual(a, b) | ||
} | ||
switch a.(type) { | ||
case int, int8, int16, int32, int64: | ||
switch b.(type) { | ||
case int, int8, int16, int32, int64: | ||
return reflect.ValueOf(a).Int() == reflect.ValueOf(b).Int() | ||
} | ||
case uint, uint8, uint16, uint32, uint64: | ||
switch b.(type) { | ||
case uint, uint8, uint16, uint32, uint64: | ||
return reflect.ValueOf(a).Uint() == reflect.ValueOf(b).Uint() | ||
} | ||
case float32, float64: | ||
switch b.(type) { | ||
case float32, float64: | ||
return reflect.ValueOf(a).Float() == reflect.ValueOf(b).Float() | ||
} | ||
case string: | ||
switch b.(type) { | ||
case []byte: | ||
return a.(string) == string(b.([]byte)) | ||
} | ||
case []byte: | ||
switch b.(type) { | ||
case string: | ||
return b.(string) == string(a.([]byte)) | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package mars | ||
package testing | ||
|
||
import ( | ||
"reflect" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.