-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
13 changed files
with
1,357 additions
and
171 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
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,28 @@ | ||
// Package reflection reflect stuff | ||
package reflection | ||
|
||
import "reflect" | ||
|
||
func GetType(input interface{}) string { | ||
if t := reflect.TypeOf(input); t.Kind() == reflect.Ptr { | ||
return "*" + t.Elem().Name() | ||
} else { | ||
return t.Name() | ||
} | ||
} | ||
|
||
func HasElement(input interface{}, search interface{}) bool { | ||
theValue := reflect.ValueOf(input) | ||
|
||
if theValue.Kind() == reflect.Slice { | ||
for i := 0; i < theValue.Len(); i++ { | ||
if theValue.Index(i).CanSet() { | ||
if theValue.Index(i).Interface() == search { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package reflection_test | ||
|
||
import ( | ||
testing "testing" | ||
|
||
reflection "github.com/beckend/go-config/pkg/reflection" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestPkg(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "pkg reflection Suite") | ||
} | ||
|
||
var _ = Describe("pkg reflection", func() { | ||
Context("GetType", func() { | ||
When("named struct", func() { | ||
It("pointer outputs correct name", func() { | ||
type OTest struct{} | ||
Expect(reflection.GetType(&OTest{})).To(Equal("*OTest")) | ||
}) | ||
|
||
It("non pointer outputs correct name", func() { | ||
type OTest struct{} | ||
Expect(reflection.GetType(OTest{})).To(Equal("OTest")) | ||
}) | ||
}) | ||
|
||
When("unnamed non struct", func() { | ||
It("pointer outputs correct name", func() { | ||
var input map[string]interface{} | ||
Expect(reflection.GetType(&input)).To(Equal("*")) | ||
}) | ||
|
||
It("unnamed non struct non pointer outputs correct name", func() { | ||
var input map[string]interface{} | ||
Expect(reflection.GetType(input)).To(Equal("")) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("HasElement", func() { | ||
It("works for slices", func() { | ||
collection := []string{"hello", "my", "friend"} | ||
Expect(reflection.HasElement(collection, "friend")).To(Equal(true)) | ||
Expect(reflection.HasElement(collection, "apple")).To(Equal(false)) | ||
}) | ||
}) | ||
}) |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.