Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcaf8248

Browse files
author
Stephen Gutekanst
authored
qualify literal values to valast.Addr when required (#19)
* add test demonstrating valast.Addr type qualification issue#15* qualify literal values to valast.Addr when requiredFixes#15Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
1 parentd23d240 commitcaf8248

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct {
2+
a *float32
3+
b *int32
4+
c *int64
5+
d *uint32
6+
e *uint64
7+
f *int
8+
g *uint
9+
h *string
10+
i *float64
11+
}{
12+
a: valast.Addr(float32(3607)).(*float32), b: valast.Addr(int32(3607)).(*int32),
13+
c: valast.Addr(int64(3607)).(*int64),
14+
d: valast.Addr(uint32(3607)).(*uint32),
15+
e: valast.Addr(uint64(3607)).(*uint64),
16+
f: valast.Addr(3607).(*int),
17+
g: valast.Addr(uint(3607)).(*uint),
18+
h: valast.Addr("foobar").(*string),
19+
i: valast.Addr(3.14).(*float64),
20+
}

‎valast.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"go/format"
88
"go/token"
99
"io"
10+
"math"
1011
"os"
1112
"reflect"
1213
"sort"
@@ -490,13 +491,17 @@ func computeAST(v reflect.Value, opt *Options, cycleDetector *cycleDetector, pro
490491
// cyclic data structure detected
491492
returnResult{AST:ast.NewIdent("nil")},nil
492493
}
493-
elem,err:=computeASTProfiled(vv.Elem(),opt,cycleDetector,profiler,typeExprCache,packagesFound)
494-
iferr!=nil {
495-
returnResult{},err
496-
}
497-
cycleDetector.pop(vv.Interface())
498494

499495
if!isPtrToInterface&&!isAddressableKind(vv.Elem().Kind()) {
496+
ifopt.Unqualify&&literalNeedsQualification(vv.Elem()) {
497+
opt.Unqualify=false// the value must have qualification
498+
}
499+
elem,err:=computeASTProfiled(vv.Elem(),opt,cycleDetector,profiler,typeExprCache,packagesFound)
500+
iferr!=nil {
501+
returnResult{},err
502+
}
503+
cycleDetector.pop(vv.Interface())
504+
500505
// Pointers to unaddressable values can be created with help from valast.Addr.
501506
packagesFound["github.com/hexops/valast"]=true
502507
returnResult{
@@ -514,6 +519,12 @@ func computeAST(v reflect.Value, opt *Options, cycleDetector *cycleDetector, pro
514519
OmittedUnexported:elem.OmittedUnexported,
515520
},nil
516521
}
522+
523+
elem,err:=computeASTProfiled(vv.Elem(),opt,cycleDetector,profiler,typeExprCache,packagesFound)
524+
iferr!=nil {
525+
returnResult{},err
526+
}
527+
cycleDetector.pop(vv.Interface())
517528
ifisPtrToInterface {
518529
// Pointers to interfaces can be created with help from valast.AddrInterface.
519530
returnResult{
@@ -662,6 +673,38 @@ func computeAST(v reflect.Value, opt *Options, cycleDetector *cycleDetector, pro
662673
}
663674
}
664675

676+
// literalNeedsQualification tells if a literal value needs qualification or not when initializing
677+
// a value of type `interface{}`, e.g. being passed into the valast.Addr() helper function.
678+
funcliteralNeedsQualification(v reflect.Value)bool {
679+
k:=v.Kind()
680+
681+
// Simple cases: Types whose literal values are always implicitly qualified
682+
ifk==reflect.Bool||
683+
k==reflect.String||
684+
k==reflect.Int||
685+
k==reflect.Array||
686+
k==reflect.Chan||
687+
k==reflect.Func||
688+
k==reflect.Interface||
689+
k==reflect.Map||
690+
k==reflect.Ptr||
691+
k==reflect.Slice||
692+
k==reflect.Struct||
693+
k==reflect.UnsafePointer {
694+
returnfalse
695+
}
696+
697+
// Floats. If passed to a function accepting an `interface{}` value:
698+
//
699+
// * A whole number `1234` would be considered an integer.
700+
// * A non-whole number `3.14` would be considered `float64`
701+
//
702+
ifk==reflect.Float64&&v.Float()!=math.Trunc(v.Float()) {
703+
returnfalse// A float64 and not a whole number, so no qualification needed.
704+
}
705+
returntrue// needs qualification
706+
}
707+
665708
funcunexported(v reflect.Value) reflect.Value {
666709
ifv== (reflect.Value{}) {
667710
returnv

‎valast_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,32 @@ func TestPackages(t *testing.T) {
10311031
}
10321032
}
10331033

1034+
funcTestIssue15_addr_values_must_be_qualified(t*testing.T) {
1035+
f32:=float32(3607)
1036+
i32:=int32(3607)
1037+
i64:=int64(3607)
1038+
u32:=uint32(3607)
1039+
u64:=uint64(3607)
1040+
i:=int(3607)
1041+
u:=uint(3607)
1042+
str:="foobar"
1043+
f64NonInteger:=3.14
1044+
input:=struct {
1045+
a*float32
1046+
b*int32
1047+
c*int64
1048+
d*uint32
1049+
e*uint64
1050+
f*int
1051+
g*uint
1052+
h*string
1053+
i*float64
1054+
}{&f32,&i32,&i64,&u32,&u64,&i,&u,&str,&f64NonInteger}
1055+
1056+
got:=String(input)
1057+
autogold.Equal(t,got)
1058+
}
1059+
10341060
funcBenchmarkComplexType(b*testing.B) {
10351061
v:= test.ComplexNode{
10361062
Left:&test.ComplexNode{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp