File: fields.go

package info (click to toggle)
golang-github-scaleway-scaleway-sdk-go 1.0.0~beta32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,444 kB
  • sloc: sh: 70; makefile: 3
file content (17 lines) | stat: -rw-r--r-- 423 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package generic

import "reflect"

// HasField returns true if given struct has a field with given name
// Also allow a slice, it will use the underlying type
func HasField(i interface{}, fieldName string) bool {
	value := reflect.Indirect(reflect.ValueOf(i))
	typ := value.Type()

	if value.Kind() == reflect.Slice {
		typ = indirectType(typ.Elem())
	}

	_, fieldExists := typ.FieldByName(fieldName)
	return fieldExists
}