github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/areflect/force_test.go (about) 1 // Copyright (c) 2016 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 package areflect 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 type embedme struct { 13 } 14 15 type somestruct struct { 16 a uint32 17 embedme 18 } 19 20 func TestForcePublic(t *testing.T) { 21 c := somestruct{a: 42} 22 v := reflect.ValueOf(c) 23 // Without the call to forceExport(), the following line would crash with 24 // "panic: reflect.Value.Interface: cannot return value obtained from 25 // unexported field or method". 26 a := ForceExport(v.FieldByName("a")).Interface() 27 if i, ok := a.(uint32); !ok { 28 t.Fatalf("Should have gotten a uint32 but got a %T", a) 29 } else if i != 42 { 30 t.Fatalf("Should have gotten 42 but got a %d", i) 31 } 32 e := ForceExport(v.FieldByName("embedme")).Interface() 33 if _, ok := e.(embedme); !ok { 34 t.Fatalf("Should have gotten a embedme but got a %T", e) 35 } 36 }