File: host_source_gen.go

package info (click to toggle)
golang-github-gocql-gocql 0.0~git20191102.0.9faa4c0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,012 kB
  • sloc: sh: 84; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// +build genhostinfo

package main

import (
	"fmt"
	"reflect"
	"sync"

	"github.com/gocql/gocql"
)

func gen(clause, field string) {
	fmt.Printf("if h.%s == %s {\n", field, clause)
	fmt.Printf("\th.%s = from.%s\n", field, field)
	fmt.Println("}")
}

func main() {
	t := reflect.ValueOf(&gocql.HostInfo{}).Elem().Type()
	mu := reflect.TypeOf(sync.RWMutex{})

	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		if f.Type == mu {
			continue
		}

		switch f.Type.Kind() {
		case reflect.Slice:
			gen("nil", f.Name)
		case reflect.String:
			gen(`""`, f.Name)
		case reflect.Int:
			gen("0", f.Name)
		case reflect.Struct:
			gen("("+f.Type.Name()+"{})", f.Name)
		case reflect.Bool, reflect.Int32:
			continue
		default:
			panic(fmt.Sprintf("unknown field: %s", f))
		}
	}

}