mirror of
https://gitee.com/rulego/streamsql.git
synced 2026-03-14 14:27:27 +00:00
a47748d4c7
- Convert all Chinese function comments to English in functions package - Update interface documentation for better international readability - Maintain original logic and functionality unchanged - Improve code documentation standards for global development
28 lines
604 B
Go
28 lines
604 B
Go
package reflectutil
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
// SafeFieldByName safely gets struct field
|
|
func SafeFieldByName(v reflect.Value, fieldName string) (reflect.Value, error) {
|
|
// Check if Value is valid
|
|
if !v.IsValid() {
|
|
return reflect.Value{}, fmt.Errorf("invalid value")
|
|
}
|
|
|
|
// Check if it's a struct type
|
|
if v.Kind() != reflect.Struct {
|
|
return reflect.Value{}, fmt.Errorf("value is not a struct, got %v", v.Kind())
|
|
}
|
|
|
|
// Safely get field
|
|
field := v.FieldByName(fieldName)
|
|
if !field.IsValid() {
|
|
return reflect.Value{}, fmt.Errorf("field %s not found", fieldName)
|
|
}
|
|
|
|
return field, nil
|
|
}
|