Ejemplo n.º 1
0
func ExampleGetFieldKind() {
	s := MyStruct{
		FirstField:  "first value",
		SecondField: 2,
		ThirdField:  "third value",
	}

	var firstFieldKind reflect.Kind
	var secondFieldKind reflect.Kind
	var err error

	// GetFieldKind will return reflect.String
	firstFieldKind, err = reflections.GetFieldKind(s, "FirstField")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(firstFieldKind)

	// GetFieldKind will return reflect.Int
	secondFieldKind, err = reflections.GetFieldKind(s, "SecondField")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(secondFieldKind)
}
Ejemplo n.º 2
0
func validFieldType(fieldName string, target interface{}, fieldType string) bool {
	fieldKind, err := reflections.GetFieldKind(target, fieldName)

	if err != nil {
		return false
	}

	return fieldKind == fieldKindFromFieldType(fieldType)

}
Ejemplo n.º 3
0
func formTagFromField(fieldName string, target interface{}) (string, error) {
	fieldKind, err := reflections.GetFieldKind(target, fieldName)
	f, _ := reflections.GetField(target, fieldName)
	fmt.Printf("Field %v\n", f)
	if err != nil {
		return "", err
	}

	choices, _ := reflections.GetFieldTag(target, fieldName, "choices")

	switch fieldKind {
	case reflect.String:
		if choices != "" {
			return "list", nil
		}
		return "input", nil
	case reflect.Slice:

		return "checkbox", nil
	case reflect.Bool:
		return "confirm", nil
	}
	return "", nil
}