File: generator.go

package info (click to toggle)
golang-github-traefik-paerser 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 612 kB
  • sloc: makefile: 14
file content (97 lines) | stat: -rw-r--r-- 2,112 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Package generator implements the custom initialization of all the fields of an empty interface.
package generator

import (
	"reflect"

	"github.com/traefik/paerser/parser"
)

type initializer interface {
	SetDefaults()
}

// Generate recursively initializes an empty structure, calling SetDefaults on each field, when it applies.
func Generate(element interface{}) {
	if element == nil {
		return
	}

	generate(element)
}

func generate(element interface{}) {
	field := reflect.ValueOf(element)

	fill(field)
}

func fill(field reflect.Value) {
	switch field.Kind() {
	case reflect.Pointer:
		setPtr(field)
	case reflect.Struct:
		setStruct(field)
	case reflect.Map:
		setMap(field)
	case reflect.Slice:
		if field.Type().Elem().Kind() == reflect.Struct ||
			field.Type().Elem().Kind() == reflect.Pointer && field.Type().Elem().Elem().Kind() == reflect.Struct {
			slice := reflect.MakeSlice(field.Type(), 1, 1)
			field.Set(slice)

			// use Ptr to allow "SetDefaults"
			value := reflect.New(reflect.PointerTo(field.Type().Elem()))
			setPtr(value)

			elem := value.Elem().Elem()
			field.Index(0).Set(elem)
		} else if field.Len() == 0 {
			slice := reflect.MakeSlice(field.Type(), 0, 0)
			field.Set(slice)
		}
	}
}

func setPtr(field reflect.Value) {
	if field.IsNil() {
		field.Set(reflect.New(field.Type().Elem()))
	}

	if field.Type().Implements(reflect.TypeOf((*initializer)(nil)).Elem()) {
		method := field.MethodByName("SetDefaults")
		if method.IsValid() {
			method.Call([]reflect.Value{})
		}
	}

	fill(field.Elem())
}

func setStruct(field reflect.Value) {
	for i := 0; i < field.NumField(); i++ {
		fd := field.Field(i)
		structField := field.Type().Field(i)

		if structField.Tag.Get(parser.TagLabel) == "-" {
			continue
		}

		if parser.IsExported(structField) {
			fill(fd)
		}
	}
}

func setMap(field reflect.Value) {
	if field.IsNil() {
		field.Set(reflect.MakeMap(field.Type()))
	}

	ptrValue := reflect.New(reflect.PointerTo(field.Type().Elem()))
	fill(ptrValue)

	value := ptrValue.Elem().Elem()
	key := reflect.ValueOf(parser.MapNamePlaceholder)
	field.SetMapIndex(key, value)
}