mirror of
https://github.com/lxc/incus
synced 2026-08-02 05:26:46 +00:00
global: Use reflect.TypeFor
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
This commit is contained in:
parent
5003a4e3c1
commit
eceb86e9cf
@ -1622,7 +1622,7 @@ func prepareNetworkServerFilters(filters []string) []string {
|
||||
firstPart = strings.Split(key, ".")[0]
|
||||
}
|
||||
|
||||
if !structHasField(reflect.TypeOf(api.Network{}), firstPart) {
|
||||
if !structHasField(reflect.TypeFor[api.Network](), firstPart) {
|
||||
filter = fmt.Sprintf("config.%s", filter)
|
||||
}
|
||||
|
||||
|
||||
@ -850,7 +850,7 @@ func (c *cmdNetworkACLRule) commandAdd() *cobra.Command {
|
||||
// networkACLRuleJSONStructFieldMap returns a map of JSON tag names to struct field indices for api.NetworkACLRule.
|
||||
func networkACLRuleJSONStructFieldMap() map[string]int {
|
||||
// Use reflect to get field names in rule from json tags.
|
||||
ruleType := reflect.TypeOf(api.NetworkACLRule{})
|
||||
ruleType := reflect.TypeFor[api.NetworkACLRule]()
|
||||
allowedKeys := make(map[string]int, ruleType.NumField())
|
||||
|
||||
for i := range ruleType.NumField() {
|
||||
|
||||
@ -16,7 +16,7 @@ import (
|
||||
// stringToTimeHookFunc is a custom decoding hook that converts string values to time.Time using the given layout.
|
||||
func stringToTimeHookFunc(layout string) mapstructure.DecodeHookFuncType {
|
||||
return func(from reflect.Type, to reflect.Type, data any) (any, error) {
|
||||
if from.Kind() == reflect.String && to == reflect.TypeOf(time.Time{}) {
|
||||
if from.Kind() == reflect.String && to == reflect.TypeFor[time.Time]() {
|
||||
strValue, ok := data.(string)
|
||||
if !ok {
|
||||
return nil, errors.New("Unexpected data type")
|
||||
|
||||
@ -20,7 +20,7 @@ func (s *utilsPropertiesTestSuite) TestStringToTimeHookFuncValidData() {
|
||||
layout := time.RFC3339
|
||||
hook := stringToTimeHookFunc(layout)
|
||||
|
||||
result, err := hook(reflect.TypeOf(""), reflect.TypeOf(time.Time{}), "2023-07-12T07:34:00Z")
|
||||
result, err := hook(reflect.TypeFor[string](), reflect.TypeFor[time.Time](), "2023-07-12T07:34:00Z")
|
||||
s.NoError(err)
|
||||
s.Equal(time.Date(2023, 7, 12, 7, 34, 0, 0, time.UTC), result)
|
||||
}
|
||||
@ -29,7 +29,7 @@ func (s *utilsPropertiesTestSuite) TestStringToTimeHookFuncInvalidData() {
|
||||
layout := time.RFC3339
|
||||
hook := stringToTimeHookFunc(layout)
|
||||
|
||||
_, err := hook(reflect.TypeOf(""), reflect.TypeOf(time.Time{}), "not a time")
|
||||
_, err := hook(reflect.TypeFor[string](), reflect.TypeFor[time.Time](), "not a time")
|
||||
s.Error(err, "Expected an error but got nil")
|
||||
}
|
||||
|
||||
|
||||
@ -68,9 +68,9 @@ func (s *utilsTestSuite) TestgetExistingAliasesEmpty() {
|
||||
}
|
||||
|
||||
func (s *utilsTestSuite) TestStructHasFields() {
|
||||
s.Equal(structHasField(reflect.TypeOf(api.Image{}), "type"), true)
|
||||
s.Equal(structHasField(reflect.TypeOf(api.Image{}), "public"), true)
|
||||
s.Equal(structHasField(reflect.TypeOf(api.Image{}), "foo"), false)
|
||||
s.Equal(structHasField(reflect.TypeFor[api.Image](), "type"), true)
|
||||
s.Equal(structHasField(reflect.TypeFor[api.Image](), "public"), true)
|
||||
s.Equal(structHasField(reflect.TypeFor[api.Image](), "foo"), false)
|
||||
}
|
||||
|
||||
func (s *utilsTestSuite) TestGetServerSupportedFilters() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user