Skip to content

Commit b703a20

Browse files
committed
feat: pointer
1 parent c89626c commit b703a20

File tree

7 files changed

+636
-0
lines changed

7 files changed

+636
-0
lines changed

tools/pointer/bool.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package pointer
2+
3+
// ToBoolPointer 将布尔变量转换为布尔指针
4+
func ToBoolPointer(boolValue bool) *bool {
5+
return &boolValue
6+
}
7+
8+
// ToBoolPointerOrNilIfFalse 将布尔变量转换为布尔类型的指针,如果变量的值为false的话则转换为nil指针
9+
func ToBoolPointerOrNilIfFalse(boolValue bool) *bool {
10+
if boolValue {
11+
return &boolValue
12+
}
13+
return nil
14+
}
15+
16+
// FromBoolPointer 获取布尔指针实际指向的值,如果指针为nil的话则返回false
17+
func FromBoolPointer(boolPointer *bool) bool {
18+
return FromBoolPointerOrDefault(boolPointer, false)
19+
}
20+
21+
// FromBoolPointerOrDefault 获取布尔指针实际指向的值,如果指针为nil的话则返回defaultValue
22+
func FromBoolPointerOrDefault(boolPointer *bool, defaultValue bool) bool {
23+
if boolPointer == nil {
24+
return defaultValue
25+
} else {
26+
return *boolPointer
27+
}
28+
}

tools/pointer/string.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pointer
2+
3+
// ToStringPointer 将string类型的变量转换为对应的*string指针类型
4+
func ToStringPointer(v string) *string {
5+
return &v
6+
}
7+
8+
// ToStringPointerOrNilIfEmpty 将string类型的变量转换为对应的*string指针类型,如果变量的值为空字符串的话则返回nil指针
9+
func ToStringPointerOrNilIfEmpty(v string) *string {
10+
if v == "" {
11+
return nil
12+
}
13+
return &v
14+
}
15+
16+
// FromStringPointer 获取*string类型的指针的实际值,如果指针为nil的话则返回空字符串
17+
func FromStringPointer(p *string) string {
18+
return FromStringPointerOrDefaultIfNil(p, "")
19+
}
20+
21+
// FromStringPointerOrDefaultIfNil 获取*string类型的指针的实际值,如果指针为nil的话则返回defaultValue
22+
func FromStringPointerOrDefaultIfNil(v *string, defaultValue string) string {
23+
if v == nil {
24+
return defaultValue
25+
}
26+
return *v
27+
}

tools/pointer/template.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pointer
2+
3+
// To{{upper}}Pointer 将{{lower}}类型的变量转换为对应的*{{lower}}指针类型
4+
func To{{upper}}Pointer(v {{lower}}) *{{lower}} {
5+
return &v
6+
}
7+
8+
// To{{upper}}PointerOrNilIfZero 将{{lower}}类型的变量转换为对应的*{{lower}}指针类型,如果变量的值为0的话则返回nil指针
9+
func To{{upper}}PointerOrNilIfZero(v {{lower}}) *{{lower}} {
10+
if v == 0 {
11+
return nil
12+
}
13+
return &v
14+
}
15+
16+
// From{{upper}}Pointer 获取*{{lower}}类型的指针的实际值,如果指针为nil的话则返回0
17+
func From{{upper}}Pointer(p *{{lower}}) {{lower}} {
18+
return From{{upper}}PointerOrDefaultIfNil(p, 0)
19+
}
20+
21+
// From{{upper}}PointerOrDefaultIfNil 获取*{{lower}}类型的指针的实际值,如果指针为nil的话则返回defaultValue
22+
func From{{upper}}PointerOrDefaultIfNil(v *{{lower}}, defaultValue {{lower}}) {{lower}} {
23+
if v == nil {
24+
return defaultValue
25+
}
26+
return *v
27+
}

tools/pointer/time_duration.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package pointer
2+
3+
import "time"
4+
5+
// ToDurationPointer 将time.Duration类型的变量转换为对应的*time.Duration指针类型
6+
func ToDurationPointer(v time.Duration) *time.Duration {
7+
return &v
8+
}
9+
10+
// ToDurationPointerOrNilIfZero 将time.Duration类型的变量转换为对应的*time.Duration指针类型,如果变量的值为0的话则返回nil指针
11+
func ToDurationPointerOrNilIfZero(v time.Duration) *time.Duration {
12+
if v == 0 {
13+
return nil
14+
}
15+
return &v
16+
}
17+
18+
// FromDurationPointer 获取*time.Duration类型的指针的实际值,如果指针为nil的话则返回0
19+
func FromDurationPointer(p *time.Duration) time.Duration {
20+
return FromDurationPointerOrDefaultIfNil(p, 0)
21+
}
22+
23+
// FromDurationPointerOrDefaultIfNil 获取*time.Duration类型的指针的实际值,如果指针为nil的话则返回defaultValue
24+
func FromDurationPointerOrDefaultIfNil(v *time.Duration, defaultValue time.Duration) time.Duration {
25+
if v == nil {
26+
return defaultValue
27+
}
28+
return *v
29+
}

tools/pointer/time_time.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package pointer
2+
3+
import "time"
4+
5+
// NowTimePointer 返回当前时间的指针表示形式 *time.Time
6+
func NowTimePointer() *time.Time {
7+
f := time.Now()
8+
return &f
9+
}
10+
11+
// ToTimePointer 将time.Time类型转换为指针,如果时间为零值则返回空指针
12+
func ToTimePointer(t time.Time) *time.Time {
13+
if t.IsZero() {
14+
return nil
15+
}
16+
return &t
17+
}
18+
19+
// FromTimePointer 从time.Time类型的指针中读取时间,如果为空指针,则读取到零值
20+
func FromTimePointer(t *time.Time) time.Time {
21+
return FromTimePointerOrDefault(t, time.Time{})
22+
}
23+
24+
// FromTimePointerOrDefault 从time.Time类型的指针中读取时间,如果为空指针,则返回defaultValue
25+
func FromTimePointerOrDefault(t *time.Time, defaultValue time.Time) time.Time {
26+
if t == nil {
27+
return defaultValue
28+
}
29+
return *t
30+
}

tools/pointer/type_gen.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//go:build ignore
2+
// +build ignore
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"go/format"
9+
"io/ioutil"
10+
"os"
11+
"strings"
12+
)
13+
14+
var packageName = "package pointer"
15+
16+
func main() {
17+
f, err := os.Open("template.txt")
18+
if err != nil {
19+
panic(err)
20+
}
21+
fileData, err := ioutil.ReadAll(f)
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
w := new(bytes.Buffer)
27+
start_pos := strings.Index(string(fileData), packageName)
28+
w.WriteString(string(fileData)[start_pos : start_pos+len(packageName)])
29+
30+
ts := []string{"Byte", "Complex64", "Complex128", "Float32", "Float64", "Int", "Int8", "Int16", "Int32", "Int64", "Rune", "Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr"}
31+
32+
for _, upper := range ts {
33+
lower := strings.ToLower(upper)
34+
data := string(fileData)
35+
36+
data = data[start_pos+len(packageName):]
37+
38+
data = strings.Replace(data, "{{upper}}", upper, -1)
39+
data = strings.Replace(data, "{{lower}}", lower, -1)
40+
41+
w.WriteString(data)
42+
w.WriteString("\r\n")
43+
}
44+
45+
out, err := format.Source(w.Bytes())
46+
if err != nil {
47+
panic(err)
48+
}
49+
if err := ioutil.WriteFile("types.go", out, 0660); err != nil {
50+
panic(err)
51+
}
52+
}

0 commit comments

Comments
 (0)