forked from bold-commerce/protoc-gen-struct-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
67 lines (53 loc) · 1.5 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Package helpers contains manually written functions for transforming custom
// types. Such package should be written by end-user because plugin has no idea
// which types could be used. It generates just function names for non-standard
// types.
package helpers
import (
"strconv"
"time"
"github.com/alexander-sapozhnikov/protoc-gen-struct-transformer/example/nulls"
)
func TimeToNullsTime(t time.Time) nulls.Time {
return nulls.Time{Time: t}
}
func NullsTimeToTime(nt nulls.Time) time.Time {
return nt.Time
}
func TimePtrToNullsTimePtr(t *time.Time) *nulls.Time {
return &nulls.Time{Time: *t}
}
func NullsTimePtrToTimePtr(nt *nulls.Time) *time.Time {
return &nt.Time
}
func TimePtrToNullsTime(t *time.Time) nulls.Time {
return nulls.Time{Time: *t}
}
func NullsTimeToTimePtr(nt nulls.Time) *time.Time {
return &nt.Time
}
func Int32ToString(i int32) string {
return strconv.FormatInt(int64(i), 10)
}
// StringToInt32 converts string to int32. It doesn't return an error for now,
// and if string is not correct or value is aut of range of int32 it will return
// default int32 value which is 0.
func StringToInt32(s string) int32 {
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return 0
}
return int32(i)
}
func Int64ToString(i int64) string {
return strconv.FormatInt(i, 10)
}
// StringToInt64 converts string to int63. For details see comments for
// StringToInt32 function.
func StringToInt64(s string) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return i
}