Skip to content

Commit

Permalink
add support for time.Time; make valast.Ptr generic (#25)
Browse files Browse the repository at this point in the history
* add support for time.Time
* refactor: replace Addr with generic Ptr method
  • Loading branch information
michaellzc authored Aug 8, 2023
1 parent 5de3f44 commit 465506f
Show file tree
Hide file tree
Showing 42 changed files with 148 additions and 107 deletions.
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
module github.com/hexops/valast

go 1.15
go 1.20

require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/hexops/autogold v0.8.1
golang.org/x/tools v0.4.0
mvdan.cc/gofumpt v0.4.0
)

require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sys v0.3.0 // indirect
)
43 changes: 0 additions & 43 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,64 +1,21 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hexops/autogold v0.8.1 h1:wvyd/bAJ+Dy+DcE09BoLk6r4Fa5R5W+O+GUzmR985WM=
github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
mvdan.cc/gofumpt v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
mvdan.cc/gofumpt v0.4.0/go.mod h1:PljLOHDeZqgS8opHRKLzp2It2VBuSdteAgqUfzMTxlQ=
6 changes: 6 additions & 0 deletions ptr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package valast

// Ptr returns a pointer to the given value.
func Ptr[V any](v V) *V {
return &v
}
2 changes: 1 addition & 1 deletion testdata/TestEdgeCases__ptr2_to_nil_interface.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
&struct {
v **test.Bazer
}{v: valast.Addr(valast.AddrInterface(nil, (*test.Bazer)(nil)).(*test.Bazer)).(**test.Bazer)}
}{v: valast.Ptr(valast.AddrInterface(nil, (*test.Bazer)(nil)).(*test.Bazer))}
4 changes: 2 additions & 2 deletions testdata/TestEdgeCases__ptr3_to_nil_interface.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
&struct {
v ***test.Bazer
}{v: valast.Addr(valast.Addr(valast.AddrInterface(nil,
(*test.Bazer)(nil)).(*test.Bazer)).(**test.Bazer)).(***test.Bazer)}
}{v: valast.Ptr(valast.Ptr(valast.AddrInterface(nil,
(*test.Bazer)(nil)).(*test.Bazer)))}
4 changes: 2 additions & 2 deletions testdata/TestEdgeCases__ptr_to_ptr_to_interface.golden
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
&struct {
v **test.Bazer
}{v: valast.Addr(valast.AddrInterface(&test.Baz{
}{v: valast.Ptr(valast.AddrInterface(&test.Baz{
Bam: (1.34 + 0i),
zeta: &test.foo{
bar: "hello",
},
},
(*test.Bazer)(nil)).(*test.Bazer)).(**test.Bazer)}
(*test.Bazer)(nil)).(*test.Bazer))}
16 changes: 8 additions & 8 deletions testdata/TestIssue15_addr_values_must_be_qualified.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ struct {
h *string
i *float64
}{
a: valast.Addr(float32(3607)).(*float32), b: valast.Addr(int32(3607)).(*int32),
c: valast.Addr(int64(3607)).(*int64),
d: valast.Addr(uint32(3607)).(*uint32),
e: valast.Addr(uint64(3607)).(*uint64),
f: valast.Addr(3607).(*int),
g: valast.Addr(uint(3607)).(*uint),
h: valast.Addr("foobar").(*string),
i: valast.Addr(3.14).(*float64),
a: valast.Ptr(float32(3607)), b: valast.Ptr(int32(3607)),
c: valast.Ptr(int64(3607)),
d: valast.Ptr(uint32(3607)),
e: valast.Ptr(uint64(3607)),
f: valast.Ptr(3607),
g: valast.Ptr(uint(3607)),
h: valast.Ptr("foobar"),
i: valast.Ptr(3.14),
}
2 changes: 1 addition & 1 deletion testdata/TestPointers__array2.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(&[1]float32{1}).(**[1]float32)
valast.Ptr(&[1]float32{1})
2 changes: 1 addition & 1 deletion testdata/TestPointers__bool.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(true).(*bool)
valast.Ptr(true)
2 changes: 1 addition & 1 deletion testdata/TestPointers__bool2.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(valast.Addr(true).(*bool)).(**bool)
valast.Ptr(valast.Ptr(true))
2 changes: 1 addition & 1 deletion testdata/TestPointers__complex128.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(complex128((1 + 0i))).(*complex128)
valast.Ptr(complex128((1 + 0i)))
2 changes: 1 addition & 1 deletion testdata/TestPointers__complex64.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(complex64((1 + 0i))).(*complex64)
valast.Ptr(complex64((1 + 0i)))
2 changes: 1 addition & 1 deletion testdata/TestPointers__float32.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(float32(1)).(*float32)
valast.Ptr(float32(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__float64.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(float64(1)).(*float64)
valast.Ptr(float64(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__int.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(int(1)).(*int)
valast.Ptr(int(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__int16.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(int16(1)).(*int16)
valast.Ptr(int16(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__int2.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(valast.Addr(int(1)).(*int)).(**int)
valast.Ptr(valast.Ptr(int(1)))
2 changes: 1 addition & 1 deletion testdata/TestPointers__int3.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(valast.Addr(valast.Addr(int(1)).(*int)).(**int)).(***int)
valast.Ptr(valast.Ptr(valast.Ptr(int(1))))
2 changes: 1 addition & 1 deletion testdata/TestPointers__int32.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(int32(1)).(*int32)
valast.Ptr(int32(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__int64.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(int64(1)).(*int64)
valast.Ptr(int64(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__int8.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(int8(1)).(*int8)
valast.Ptr(int8(1))
4 changes: 2 additions & 2 deletions testdata/TestPointers__interface2.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
valast.Addr(valast.AddrInterface(test.Bazer{&test.Baz{
valast.Ptr(valast.AddrInterface(test.Bazer{&test.Baz{
Bam: (1.34 + 0i),
zeta: &test.foo{bar: "hello"},
}},
(*test.Bazer)(nil)).(*test.Bazer)).(**test.Bazer)
(*test.Bazer)(nil)).(*test.Bazer))
2 changes: 1 addition & 1 deletion testdata/TestPointers__map2.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(&map[string]string{"hello": "world"}).(**map[string]string)
valast.Ptr(&map[string]string{"hello": "world"})
2 changes: 1 addition & 1 deletion testdata/TestPointers__pointer.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(valast.Addr(uint(1)).(*uint)).(**uint)
valast.Ptr(valast.Ptr(uint(1)))
2 changes: 1 addition & 1 deletion testdata/TestPointers__pointer2.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(valast.Addr(valast.Addr(uint(1)).(*uint)).(**uint)).(***uint)
valast.Ptr(valast.Ptr(valast.Ptr(uint(1))))
2 changes: 1 addition & 1 deletion testdata/TestPointers__slice2.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(&[]int{1, 2, 3}).(**[]int)
valast.Ptr(&[]int{1, 2, 3})
2 changes: 1 addition & 1 deletion testdata/TestPointers__string.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr("hello world").(*string)
valast.Ptr("hello world")
6 changes: 2 additions & 4 deletions testdata/TestPointers__struct2.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
valast.Addr(&struct {
valast.Ptr(&struct {
A string
}{A: "hello world"}).(**struct {
A string
})
}{A: "hello world"})
1 change: 1 addition & 0 deletions testdata/TestPointers__time_local.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
valast.Ptr(time.Date(2016, 1, 2, 15, 4, 5, 0, time.Local))
1 change: 1 addition & 0 deletions testdata/TestPointers__time_utc.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
valast.Ptr(time.Date(2016, 1, 2, 15, 4, 5, 0, time.UTC))
2 changes: 1 addition & 1 deletion testdata/TestPointers__uint.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(uint(1)).(*uint)
valast.Ptr(uint(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__uint16.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(uint16(1)).(*uint16)
valast.Ptr(uint16(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__uint32.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(uint32(1)).(*uint32)
valast.Ptr(uint32(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__uint64.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(uint64(1)).(*uint64)
valast.Ptr(uint64(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__uint8.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(uint8(1)).(*uint8)
valast.Ptr(uint8(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__uintptr.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(uintptr(1)).(*uintptr)
valast.Ptr(uintptr(1))
2 changes: 1 addition & 1 deletion testdata/TestPointers__unsafe_pointer.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(unsafe.Pointer(uintptr(0xdeadbeef))).(*unsafe.Pointer)
valast.Ptr(unsafe.Pointer(uintptr(0xdeadbeef)))
2 changes: 1 addition & 1 deletion testdata/TestPointers__unsafe_pointer2.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
valast.Addr(valast.Addr(unsafe.Pointer(uintptr(0xdeadbeef))).(*unsafe.Pointer)).(**unsafe.Pointer)
valast.Ptr(valast.Ptr(unsafe.Pointer(uintptr(0xdeadbeef))))
1 change: 1 addition & 0 deletions testdata/TestString__time_local.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
time.Date(2016, 1, 2, 15, 4, 5, 0, time.Local)
1 change: 1 addition & 0 deletions testdata/TestString__time_utc.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
time.Date(2016, 1, 2, 15, 4, 5, 0, time.UTC)
Loading

0 comments on commit 465506f

Please sign in to comment.