1// +build genhostinfo 2 3package main 4 5import ( 6 "fmt" 7 "reflect" 8 "sync" 9 10 "github.com/gocql/gocql" 11) 12 13func gen(clause, field string) { 14 fmt.Printf("if h.%s == %s {\n", field, clause) 15 fmt.Printf("\th.%s = from.%s\n", field, field) 16 fmt.Println("}") 17} 18 19func main() { 20 t := reflect.ValueOf(&gocql.HostInfo{}).Elem().Type() 21 mu := reflect.TypeOf(sync.RWMutex{}) 22 23 for i := 0; i < t.NumField(); i++ { 24 f := t.Field(i) 25 if f.Type == mu { 26 continue 27 } 28 29 switch f.Type.Kind() { 30 case reflect.Slice: 31 gen("nil", f.Name) 32 case reflect.String: 33 gen(`""`, f.Name) 34 case reflect.Int: 35 gen("0", f.Name) 36 case reflect.Struct: 37 gen("("+f.Type.Name()+"{})", f.Name) 38 case reflect.Bool, reflect.Int32: 39 continue 40 default: 41 panic(fmt.Sprintf("unknown field: %s", f)) 42 } 43 } 44 45} 46