Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion tests/types/ff/everything.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,65 @@

package ff

import (
"regexp"
"runtime"
"strconv"
)

var ExpectedSomethingValue int8
var GoLangVersionPre16 bool

func init() {
// since go1.6 reflect package changed behaivour:
//
// --------
// https://tip.golang.org/doc/go1.6
//
// The reflect package has resolved a long-standing incompatibility between
// the gc and gccgo toolchains regarding embedded unexported struct types
// containing exported fields. Code that walks data structures using
// reflection, especially to implement serialization in the spirit of the
// encoding/json and encoding/xml packages, may need to be updated.
//
// The problem arises when using reflection to walk through an embedded
// unexported struct-typed field into an exported field of that struct. In
// this case, reflect had incorrectly reported the embedded field as exported,
// by returning an empty Field.PkgPath. Now it correctly reports the field as
// unexported but ignores that fact when evaluating access to exported fields
// contained within the struct.
//
// Updating: Typically, code that previously walked over structs and used
//
// f.PkgPath != ""
// to exclude inaccessible fields should now use
//
// f.PkgPath != "" && !f.Anonymous
// For example, see the changes to the implementations of encoding/json and
// encoding/xml.
//
// --------
//
// I didn't find better option to get Go's version rather then parsing
// runtime.Version(). Godoc say that Version() can return multiple things:
//
// Version returns the Go tree's version string. It is either the commit
// hash and date at the time of the build or, when possible, a release tag
// like "go1.3".
//
// So, I'll assumes that if Version() returns not a release tag, running
// version is younger then 1.5. Patches welcome :-)

versionRegexp := regexp.MustCompile("^go[0-9]+\\.([0-9]+)")
if res := versionRegexp.FindStringSubmatch(runtime.Version()); len(res) > 1 {
if i, _ := strconv.Atoi(res[1]); i < 6 {
// pre go1.6
GoLangVersionPre16 = true
ExpectedSomethingValue = 99
}
}
}

type SweetInterface interface {
Cats() int
}
Expand Down Expand Up @@ -93,7 +152,7 @@ func NewEverything(e *Everything) {
}
e.String = "snowman->☃"
e.FooStruct = &Foo{Bar: 1}
e.Something = 99
e.Something = ExpectedSomethingValue
e.MySweetInterface = &Cats{}
e.MapMap = map[string]map[string]string{
"a": map[string]string{"b": "2", "c": "3", "d": "4"},
Expand Down
7 changes: 4 additions & 3 deletions tests/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestRoundTrip(t *testing.T) {
t.Fatal("Embeded struct didn't Unmarshal")
}

if recordTripped.Something != 99 {
if recordTripped.Something != ff.ExpectedSomethingValue {
t.Fatal("Embeded nonexported-struct didn't Unmarshal")
}
}
Expand Down Expand Up @@ -119,8 +119,9 @@ func TestUnmarshalFull(t *testing.T) {
t.Fatalf("record.String decoding problem, expected: %v got: %v", expect, record.String)
}

if record.Something != 99 {
t.Fatalf("record.Something decoding problem, expected: 99 got: %v", record.Something)
if record.Something != ff.ExpectedSomethingValue {
t.Fatalf("record.Something decoding problem, expected: %d got: %v",
ff.ExpectedSomethingValue, record.Something)
}
}

Expand Down