Skip to content

Commit

Permalink
add helper function for converting basic type (knative#193)
Browse files Browse the repository at this point in the history
* add helper function for converting basic type

Co-authored-by: fm <mingfu@alauda.cn>
  • Loading branch information
nanjingfm and fm authored Apr 2, 2022
1 parent fb53968 commit d52caa2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pointer/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2021 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pointer

// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }

// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }

// Int64 is a helper routine that allocates a new int64 value
// to store v and returns a pointer to it.
func Int64(v int64) *int64 { return &v }

// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }
47 changes: 47 additions & 0 deletions pointer/pointer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2021 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pointer

import (
"testing"

. "github.com/onsi/gomega"
)

func TestBool(t *testing.T) {
g := NewGomegaWithT(t)
input := true
g.Expect(Bool(input)).To(Equal(&input))
}

func TestInt(t *testing.T) {
g := NewGomegaWithT(t)
input := int(0)
g.Expect(Int(input)).To(Equal(&input))
}

func TestInt64(t *testing.T) {
g := NewGomegaWithT(t)
input := int64(0)
g.Expect(Int64(input)).To(Equal(&input))
}

func TestString(t *testing.T) {
g := NewGomegaWithT(t)
input := ""
g.Expect(String(input)).To(Equal(&input))
}

0 comments on commit d52caa2

Please sign in to comment.