-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes to code gen, unsafe access of unexported template data (#7)
- Loading branch information
Showing
5 changed files
with
84 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//+build appengine gopherjs purego | ||
// NB: other environments where unsafe is unappropriate should use "purego" build tag | ||
// https://github.com/golang/go/issues/23172 | ||
|
||
package gopoet | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func getField(v reflect.Value, index int) (reflect.Value, bool) { | ||
fld := v.Field(index) | ||
// We can't use unsafe, so return false for unexported fields :( | ||
return fld, !fld.IsValid() || fld.CanInterface() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//+build !appengine,!gopherjs,!purego | ||
// NB: other environments where unsafe is unappropriate should use "purego" build tag | ||
// https://github.com/golang/go/issues/23172 | ||
|
||
package gopoet | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
func getField(v reflect.Value, index int) (reflect.Value, bool) { | ||
fld := v.Field(index) | ||
if !fld.IsValid() || fld.CanInterface() { | ||
return fld, true | ||
} | ||
|
||
// NB: We are being super-sneaky. Go reflection will not let us call | ||
// fld.Interface() if fld was obtained via unexported fields (which it | ||
// was!). So we use unsafe to create an alternate reflect.Value instance | ||
// that represents the same value (same type and address). We can then | ||
// call Interface() on *that*. | ||
val := reflect.NewAt(fld.Type(), unsafe.Pointer(fld.UnsafeAddr())).Elem() | ||
return val, true | ||
} |