Consider we have an aggregation struct of interfaces
type Aggregation struct {
a InterfaceA
b InterfaceB
...
n InterfaceN
}
we are trying to make the following function to initialize this struct's fields more funky -- to eliminate the switch:
func (a *Aggregation) Register(i interface{}) *Aggregation {
switch v := i.(type) {
case InterfaceA:
a.a = v
case InterfaceB:
a.a = b
...
case InterfaceN:
a.a = v
}
return a
}
is there any way to accomplish the same functionality with reflection?
This seems to be working
func (a *Aggr) Register2(i interface{}) *Aggr {
v := reflect.ValueOf(a).Elem()
for j := 0; j < v.NumField(); j++ {
f := v.Field(j)
t := f.Type()
if reflect.TypeOf(i).Implements(t) {
f.Set(reflect.ValueOf(i))
break
}
}
return a
}
cc #jimb
Related
I wanted to loop through a struct and modify fields value using reflection. How can I Set it?
func main() {
x := struct {
Foo string
Bar int
}{"foo", 2}
StructCheck(Checker, x)
}
func Checker(s interface{}) interface{} {
log.Println(s)
return s
}
func StructCheck(check func(interface{}) interface{}, x interface{}) interface{} {
v := reflect.ValueOf(x)
for i := 0; i < v.NumField(); i++ {
r := check(v.Field(i))
w := reflect.ValueOf(&r).Elem()
log.Println(w.Type(), w.CanSet())
// v.Field(i).Set(reflect.ValueOf(w))
}
return v
}
Running Set() causes panic and shows :reflect.Value.Set using unaddressable value
You must pass an addressable value to the function.
StructCheck(Checker, &x)
There's
Dereference the value in the StructCheck:
v := reflect.ValueOf(x).Elem() // Elem() gets value of ptr
There were some other issues. Here's the updated code:
func StructCheck(check func(interface{}) interface{}, x interface{}) {
v := reflect.ValueOf(x).Elem()
for i := 0; i < v.NumField(); i++ {
r := check(v.Field(i).Interface())
v.Field(i).Set(reflect.ValueOf(r))
}
}
Run it on the Playground.
I want to take data from DB and write to excel
let's say I have a struct like:
type user struct {
ID int64
Name string
Age int
}
I can get a pointer to slice of user type form DB &[]user{}
but I want to convert that slice to a 2D slice of string [][]string{}
and here's my code try to do such job:
func toStrings(slice interface{}) [][]string {
switch reflect.TypeOf(slice).Elem().Kind() {
case reflect.Slice:
ret := [][]string{}
val := reflect.ValueOf(slice).Elem()
for i := 0; i < val.Len(); i++ {
tempSlice := []string{}
tempV := reflect.ValueOf(val.Index(i))
for j := 0; j < tempV.NumField(); j++ {
tempSlice = append(tempSlice, tempV.Field(j).String())
}
ret = append(ret, tempSlice)
}
return ret
}
return nil
}
But from the code above all I get is a slice like [<*reflect.rtype Value> <unsafe.Pointer Value> <reflect.flag Value>]
where I do it wrong?
my codes in golang playground
sorry, I found where I do it wrong, I got tempV wrong
func toStrings(slice interface{}) [][]string {
switch reflect.TypeOf(slice).Elem().Kind() {
case reflect.Slice:
ret := [][]string{}
val := reflect.ValueOf(slice).Elem()
for i := 0; i < val.Len(); i++ {
tempSlice := []string{}
// tempV should be:
tempV := val.Index(i)
// instead of reflect.ValueOf(val.Index(i))
for j := 0; j < tempV.NumField(); j++ {
tempSlice = append(tempSlice, tempV.Field(j).String())
}
ret = append(ret, tempSlice)
}
return ret
}
return nil
}
There are two problems in the code in the question. The first problem is the slice element is doubled wrapped by a a reflect.Value in the expression reflect.Value(val.Index(i)). Fix by removing the extra call to reflect.Value. The second problem is that the reflect.Value String method does not convert the underlying value to its string representation. Use fmt.Sprint (or one of its friends) to do that.
Try this:
func toStrings(slice interface{}) [][]string {
// Get reflect value for slice. Use Indirect to
// handle slice argument and pointer to slice
// argument.
v := reflect.Indirect(reflect.ValueOf(slice))
if v.Kind() != reflect.Slice {
return nil
}
var result [][]string
// For each element...
for i := 0; i < v.Len(); i++ {
// Get reflect value for slice element (a struct). Use
// Indirect to handle slice of struct and slice of
// pointer to struct.
e := reflect.Indirect(v.Index(i))
if e.Kind() != reflect.Struct {
return nil
}
// Convert fields to string and append.
var element []string
for i := 0; i < e.NumField(); i++ {
// Use fmt.Sprint to convert arbitrary Go value
// to a string.
element = append(element, fmt.Sprint(e.Field(i).Interface()))
}
result = append(result, element)
}
return result
}
Run it on the playground.
Maybe I have a simple way to resolve the problem, golang playground here
I used encoding/json to convert to json data, then convert it to map[string]interface{}.
func toStrings2(slice interface{}) [][]string {
jsonData, _ := json.Marshal(slice)
var out []map[string]interface{}
_ = json.Unmarshal(jsonData, &out)
var fields []string
if len(out) > 0 {
for k := range out[0] {
fields = append(fields, k)
}
}
var ret [][]string
for _, row := range out {
var r []string
for _, k := range fields {
r = append(r, fmt.Sprint(row[k]))
}
ret = append(ret, r)
}
return ret
}
Notice:
With the help of #CeriseLimón, I known that the code in this answer can't handle large values for User.ID.
I faced such a problem.
I need to compare two structure if they type and name of field is equal.
To assign value from sour to dist. I write some code, but here I can assign reflect.Field() value. Could you help me? And I create the test in the bellow
import (
"reflect"
"testing"
)
func Assign(sour interface{}, dist interface{}) uint {
counter := 0
source := reflect.ValueOf(sour)
target := reflect.ValueOf(dist)
typeSource := reflect.TypeOf(sour)
typeTarget := reflect.TypeOf(dist)
for i:=0; i<source.NumField(); i++{
for j:=0; j<target.NumField();j++{
if (typeSource.Field(i).Type==typeTarget.Field(j).Type && typeSource.Field(i).Name==typeTarget.Field(j).Name){
counter = counter + 1
target.FieldByName(typeSource.Field(i).Name).Set(source.Field(i))
}
}
}
return uint(counter)
}
func TestAssign(t *testing.T) {
type A struct {
A string
B uint
C string
}
type B struct {
AA string
B int
C string
}
var (
a = A{
A: "Тест A",
B: 55,
C: "Test C",
}
b = B{
AA: "OKOK",
B: 10,
C: "FAFA",
}
)
result := Assign(a, b)
switch true {
case b.B != 10:
t.Errorf("b.B = %d; need to be 10", b.B)
case b.C != "Test C":
t.Errorf("b.C = %v; need to be 'Test C'", b.C)
case result != 1:
t.Errorf("Assign(a,b) = %d; need to be 1", result)
}
}
For Assign to work, the second argument must be addressable, i.e. you need to pass a pointer to the struct value.
// the second argument MUST be a pointer to the struct
Assing(source, &target)
Then you need to slightly modify your implementation of Assign since a pointer does not have fileds. You can use the Elem() method to get the struct value to which the pointer points.
func Assign(sour interface{}, dist interface{}) uint {
counter := 0
source := reflect.ValueOf(sour)
// dist is expected to be a pointer, so use Elem() to
// get the type of the value to which the pointer points
target := reflect.ValueOf(dist).Elem()
typeSource := reflect.TypeOf(sour)
typeTarget := target.Type()
for i := 0; i < source.NumField(); i++ {
for j := 0; j < target.NumField(); j++ {
if typeSource.Field(i).Type == typeTarget.Field(j).Type && typeSource.Field(i).Name == typeTarget.Field(j).Name {
counter = counter + 1
target.FieldByName(typeSource.Field(i).Name).Set(source.Field(i))
}
}
}
return uint(counter)
}
I'm creating a utility package for my project.
Many of my string slices need a function to
1. remove duplicates
2. remove empty strings
I know 1 way to do this:1. Add a function for each case which accepts a string slice and returns a string slice
func removeEmpty(s []string) []string {
i := 0 // i points to next available pos
for _, v := range s {
if v != "" {
s[i] = v
i++
}
}
return s[:i]
}
func dedup(s []string) []string {
i := 0 // i points to next available pos
mp := map[string]bool{}
for _, v := range s {
if _, ok := mp[v]; !ok {
s[i] = v
mp[v] = true
i++
}
}
return s[:i]
}
when I apply these 2 functions to my slice, I can do:
mySlice := string[]{}
mySlice = dedup(removeEmpty(mySlice))
I want to make it somthing like:
mySlice = mySlice.dedup().removeEmpty()
or
mySlice.dedup().removeEmpty()
Maybe I can add custom method to slice[] ?
I tried writing it
func (s []string) removeEmpty() {
}
I'm getting complie error.
You can't define methods on []string, but you can define your own type based on []string and define methods on that:
type myStringSlice []string
func (s myStringSlice) removeEmpty() myStringSlice {
i := 0 // i points to next available pos
for _, v := range s {
if v != "" {
s[i] = v
i++
}
}
return s[:i]
}
func (s myStringSlice) dedup() myStringSlice {
i := 0 // i points to next available pos
mp := map[string]bool{}
for _, v := range s {
if _, ok := mp[v]; !ok {
s[i] = v
mp[v] = true
i++
}
}
return s[:i]
}
See https://play.golang.org/p/u1z_N3c_wPP.
As mentioned in the documentation:
You can only declare a method with a receiver whose type is defined in the same package as the method. You cannot declare a method with a receiver whose type is defined in another package.
So you have to declare a type on []string and then declare the method on your own type.
In Python, one can write code like this, to assign multiple values from a list:
(a, b, c, d) = [1,2,3,4]
Is there a similar set of Go library function for slices? That is, I can do this:
http://play.golang.org/p/DY1Bi5omm1
package main
func get3(s []interface{}) (
a interface{},
b interface{},
c interface{},
rest []interface{}) {
return s[0],s[1],s[2],s[4:]
}
func main() {
s := make([]interface{},5);
for i :=0 ; i < 5; i++ { s[i] = i}
a,b,c,_ := get3(s)
print(a.(int))
print(b.(int))
print(c.(int))
}
Is there a standard gophery way to do this?
And is there a way around the interface{} ugliness?
I don't think you can, not in an idiomatic/clean way at least. You CAN do multiple assignments, but you will have to pass individual values either directly or with a closure:
package main
import (
"fmt"
)
func valuesFromList(list[]int,startFrom int) func() int {
i:=startFrom
return func() int {
ret := list[i]
i++
return ret
}
}
func main () {
list := []int{0,1,2,3,4,5,6,7,8,9}
yield := valuesFromList(list,5)
//This works
a,b,c := yield(),yield(),yield()
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
//This also works
d,e,f := list[0],list[1],list[2]
fmt.Println(d)
fmt.Println(e)
fmt.Println(f)
//This won't work
//g,h,i:= list[7:9]
}
Not like that; you would need dynamic typing or parametric polymorphism, which are not available in Go. The closest I can think about is by fiddling with reflect, like this: http://play.golang.org/p/-K4jh2nZjq
// src is supposed to be []T.
// dst are supposed to be &T, except the last one, which must be a 'rest' &[]T (or nil for discarding).
// There must not be more dst vars than elements in src.
func extract(src interface{}, dst ...interface{}) {
srcV := reflect.ValueOf(src)
// Iterate over dst vars until we run out of them.
i := 0
for i = 0; i < len(dst)-1; i++ {
reflect.Indirect(reflect.ValueOf(dst[i])).Set(srcV.Index(i))
}
// Now, the rest.
restDst := dst[i]
if restDst == nil {
return
}
restV := reflect.ValueOf(restDst)
indirectRest := reflect.Indirect(restV)
l := srcV.Len() - i
indirectRest.Set(reflect.MakeSlice(restV.Type().Elem(), 0, l))
for ; i < srcV.Len(); i++ {
itemV := srcV.Index(i)
indirectRest.Set(reflect.Append(indirectRest, itemV))
}
return
}
Which then you call like:
sl := []int{1, 2, 3, 4, 5, 6} // int or any other type
var a, b, c int
var rest []int
extract(sl, &a, &b, &c, &rest)
So the ugliness doesn't get out the function.
But note that all that happens at runtime, so it's not safe nor efficient and definitely is not idiomatic Go.