Files
pmomusic/upnp/devices/services/statevariables/statevariable_cast.go

30 lines
1.3 KiB
Go
Raw Normal View History

2025-07-13 07:02:26 +02:00
package statevariables
2025-07-12 22:25:50 +02:00
// Cast transforms any given value into an interface suitable for use in a StateValue object, using
// the underlying type's specific casting rules. It will return an error if the transformation fails or
// if the provided interface is not supported by the ValueType of the StateValue.
// This function does NOT mutate the original value it receives as input.
//
// The parameter 'val' is an interface that needs to be cast into a form compatible with the internal
// state representation used by the StateValue object.
//
// It returns an interface and an error: if the casting operation was successful, the first return value will
// be the casted version of 'val', and the second one (an error) will be nil. If the casting fails, it will
// return a nil for the first value and an appropriate error for the second one.
//
// This function should not modify its input value. It is idempotent and always returns consistent results given
// the same inputs. However, if the provided interface 'val' does not match with the ValueType of StateValue, it
// will return an error.
//
// Example usage:
//
// state := upnp.NewStateValue(upnp.NewTime())
// castedVal, err := state.Cast("2022-12-31")
// if err != nil {
// log.Println(err)
// return
// }
2025-07-13 06:59:56 +02:00
func (sv *StateVariable) Cast(val interface{}) (interface{}, error) {
2025-07-12 22:25:50 +02:00
return sv.valueType.Cast(val)
}