2022-04-12 12:09:09 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
package gconv
|
|
|
|
|
|
|
|
|
|
// Convert converts the variable `fromValue` to the type `toTypeName`, the type `toTypeName` is specified by string.
|
2023-08-17 20:33:12 +08:00
|
|
|
//
|
2022-04-12 12:09:09 +08:00
|
|
|
// The optional parameter `extraParams` is used for additional necessary parameter for this conversion.
|
2023-08-17 20:33:12 +08:00
|
|
|
// It supports common basic types conversion as its conversion based on type name string.
|
2025-03-06 23:04:26 +08:00
|
|
|
func Convert(fromValue any, toTypeName string, extraParams ...any) any {
|
|
|
|
|
result, _ := defaultConverter.ConvertWithTypeName(fromValue, toTypeName, ConvertOption{
|
|
|
|
|
ExtraParams: extraParams,
|
|
|
|
|
SliceOption: SliceOption{ContinueOnError: true},
|
|
|
|
|
MapOption: MapOption{ContinueOnError: true},
|
|
|
|
|
StructOption: StructOption{ContinueOnError: true},
|
2022-04-12 12:09:09 +08:00
|
|
|
})
|
2025-03-06 23:04:26 +08:00
|
|
|
return result
|
2022-04-12 12:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-12 22:00:35 +08:00
|
|
|
// ConvertWithRefer converts the variable `fromValue` to the type referred by value `referValue`.
|
|
|
|
|
//
|
|
|
|
|
// The optional parameter `extraParams` is used for additional necessary parameter for this conversion.
|
|
|
|
|
// It supports common basic types conversion as its conversion based on type name string.
|
2025-03-06 23:04:26 +08:00
|
|
|
func ConvertWithRefer(fromValue any, referValue any, extraParams ...any) any {
|
|
|
|
|
result, _ := defaultConverter.ConvertWithRefer(fromValue, referValue, ConvertOption{
|
|
|
|
|
ExtraParams: extraParams,
|
|
|
|
|
SliceOption: SliceOption{ContinueOnError: true},
|
|
|
|
|
MapOption: MapOption{ContinueOnError: true},
|
|
|
|
|
StructOption: StructOption{ContinueOnError: true},
|
2023-09-12 22:00:35 +08:00
|
|
|
})
|
2025-03-06 23:04:26 +08:00
|
|
|
return result
|
2022-04-12 12:09:09 +08:00
|
|
|
}
|