Skip to content

Commit b1b7130

Browse files
committed
feat: template
1 parent 92b1386 commit b1b7130

File tree

6 files changed

+303
-198
lines changed

6 files changed

+303
-198
lines changed

pkg/if_expression/int64.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

pkg/if_expression/interface.go

Lines changed: 0 additions & 29 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,57 @@
11
package if_expression
22

3-
import "time"
4-
5-
// ReturnTime
6-
// @Description: if实现的三元表达式,返回结果是time.Time
3+
// Return{{upper}}
4+
// @Description: if实现的三元表达式,返回结果是{{lower}}
75
// @param boolExpression: 表达式,最终返回一个布尔值
8-
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的time.Time
9-
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的time.Time
10-
// @return time.Time: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
11-
func ReturnTime(boolExpression bool, trueReturnValue, falseReturnValue time.Time) time.Time {
6+
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的{{lower}}
7+
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的{{lower}}
8+
// @return {{lower}}: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
9+
func Return{{upper}}(boolExpression bool, trueReturnValue, falseReturnValue {{lower}}) {{lower}} {
1210
if boolExpression {
1311
return trueReturnValue
1412
} else {
1513
return falseReturnValue
1614
}
1715
}
1816

19-
// ReturnTimeSlice
20-
// @Description: if实现的三元表达式,返回结果是[]time.Time
17+
// Return{{upper}}Slice
18+
// @Description: if实现的三元表达式,返回结果是[]{{lower}}
2119
// @param boolExpression: 表达式,最终返回一个布尔值
22-
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的[]time.Time
23-
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的[]time.Time
24-
// @return []time.Time: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
25-
func ReturnTimeSlice(boolExpression bool, trueReturnValue, falseReturnValue []time.Time) []time.Time {
20+
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的[]{{lower}}
21+
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的[]{{lower}}
22+
// @return []{{lower}}: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
23+
func Return{{upper}}Slice(boolExpression bool, trueReturnValue, falseReturnValue []{{lower}}) []{{lower}} {
2624
if boolExpression {
2725
return trueReturnValue
2826
} else {
2927
return falseReturnValue
3028
}
3129
}
3230

33-
// ReturnTimePointer
34-
// @Description: if实现的三元表达式,返回结果是*time.Time
31+
// Return{{Upper}}Pointer
32+
// @Description: if实现的三元表达式,返回结果是*{{lower}}
3533
// @param boolExpression: 表达式,最终返回一个布尔值
36-
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的*time.Time
37-
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的*time.Time
38-
// @return *time.Time: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
39-
func ReturnTimePointer(boolExpression bool, trueReturnValue, falseReturnValue *time.Time) *time.Time {
34+
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的*{{lower}}
35+
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的*{{lower}}
36+
// @return *{{lower}}: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
37+
func Return{{upper}}Pointer(boolExpression bool, trueReturnValue, falseReturnValue *{{lower}}) *{{lower}} {
4038
if boolExpression {
4139
return trueReturnValue
4240
} else {
4341
return falseReturnValue
4442
}
4543
}
4644

47-
// ReturnTimePointerSlice
48-
// @Description: if实现的三元表达式,返回结果是[]*time.Time
45+
// Return{{upper}}PointerSlice
46+
// @Description: if实现的三元表达式,返回结果是[]*{{lower}}
4947
// @param boolExpression: 表达式,最终返回一个布尔值
50-
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的[]*time.Time
51-
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的[]*time.Time
52-
// @return []*time.Time: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
53-
func ReturnTimePointerSlice(boolExpression bool, trueReturnValue, falseReturnValue []*time.Time) []*time.Time {
48+
// @param trueReturnValue: 当boolExpression返回值为true的时候返回的[]*{{lower}}
49+
// @param falseReturnValue: 当boolExpression返回值为false的时候返回的[]*{{lower}}
50+
// @return []*{{lower}}: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
51+
func Return{{upper}}PointerSlice(boolExpression bool, trueReturnValue, falseReturnValue []*{{lower}}) []*{{lower}} {
5452
if boolExpression {
5553
return trueReturnValue
5654
} else {
5755
return falseReturnValue
5856
}
5957
}
60-

pkg/if_expression/time_duration.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

pkg/if_expression/type_gen.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55

66
import (
77
"bytes"
8+
"fmt"
89
"go/format"
910
"io/ioutil"
1011
"os"
@@ -14,33 +15,59 @@ import (
1415
var packageName = "package if_expression"
1516

1617
func main() {
17-
f, err := os.Open("int64.go")
18+
f, err := os.Open("template")
1819
if err != nil {
1920
panic(err)
2021
}
21-
filedata, err := ioutil.ReadAll(f)
22+
fileData, err := ioutil.ReadAll(f)
2223
if err != nil {
2324
panic(err)
2425
}
2526

2627
w := new(bytes.Buffer)
27-
start_pos := strings.Index(string(filedata), packageName)
28-
w.WriteString(string(filedata)[start_pos : start_pos+len(packageName)])
28+
start_pos := strings.Index(string(fileData), packageName)
29+
w.WriteString(string(fileData)[start_pos : start_pos+len(packageName)])
2930

30-
ts := []string{"Bool", "Byte", "Complex64", "Complex128", "Float32", "Float64", "Int", "Int8", "Int16", "Int32", "Rune", "String", "Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr"}
31+
ts := []string{"Bool", "Byte", "Complex64", "Complex128", "Float32", "Float64", "Int", "Int8", "Int16", "Int32", "Int64", "Rune", "String", "Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr"}
3132

3233
for _, upper := range ts {
3334
lower := strings.ToLower(upper)
34-
data := string(filedata)
35+
data := string(fileData)
3536

3637
data = data[start_pos+len(packageName):]
3738

38-
data = strings.Replace(data, "int64", lower, -1)
39-
data = strings.Replace(data, "Int64", upper, -1)
39+
data = strings.Replace(data, "{{upper}}", upper, -1)
40+
data = strings.Replace(data, "{{lower}}", lower, -1)
4041

4142
w.WriteString(data)
4243
w.WriteString("\r\n")
4344
}
45+
46+
comps := []struct {
47+
upper string
48+
lower string
49+
}{
50+
{"Interface", "interface{}"},
51+
{"Time", "time.Time"},
52+
{"Duration", "time.Duration"},
53+
}
54+
55+
for _, comp := range comps {
56+
57+
data := string(fileData)
58+
59+
data = data[start_pos+len(packageName):]
60+
61+
data = strings.Replace(data, "{{upper}}", comp.upper, -1)
62+
data = strings.Replace(data, "{{lower}}", comp.lower, -1)
63+
64+
w.WriteString(data)
65+
w.WriteString("\r\n")
66+
}
67+
68+
fmt.Println(">>>>>")
69+
fmt.Println(w.String())
70+
4471
out, err := format.Source(w.Bytes())
4572
if err != nil {
4673
panic(err)

0 commit comments

Comments
 (0)