host_source_gen.go 783 B

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