1 package ldmodel 2 3 import ( 4 "time" 5 6 "github.com/launchdarkly/go-sdk-common/v3/ldvalue" 7 "github.com/launchdarkly/go-semver" 8 ) 9 10 // TypeConversionMethods contains type conversion functions that are used by the evaluation 11 // engine in the parent package. They implement the defined behavior for JSON value types 12 // when used with LaunchDarkly feature flag operators that apply to a more specialized 13 // logical type, such as timestamps and semantic versions. 14 // 15 // These are defined in the ldmodel package, rather than in the parent package with the 16 // evaluation engine, for two reasons: 17 // 18 // 1. They are logically part of the LaunchDarkly data model. For instance, when a clause 19 // uses a date/time operator, that implies that the clause values must be strings or numbers 20 // in the specific format that LaunchDarkly uses for timestamps. 21 // 22 // 2. The preprocessing logic in ldmodel uses the same conversions to parse clause values as 23 // the appropriate types ahead of time. EvaluatorAccessorMethods will use the pre-parsed 24 // values if available, or else apply the conversions on the fly. 25 type TypeConversionMethods struct{} 26 27 // TypeConversions is the global entry point for TypeConversionMethods. 28 var TypeConversions TypeConversionMethods //nolint:gochecknoglobals 29 30 // ValueToTimestamp attempts to convert a JSON value to a time.Time, using the standard 31 // LaunchDarkly rules for timestamp values. 32 // 33 // If the value is a string, it is parsed according to RFC3339. If the value is a number, 34 // it is treated as integer epoch milliseconds. Any other type is invalid. 35 // 36 // The second return value is true for success or false for failure. 37 func (e TypeConversionMethods) ValueToTimestamp(value ldvalue.Value) (time.Time, bool) { 38 switch value.Type() { 39 case ldvalue.StringType: 40 return parseRFC3339TimeUTC(value.StringValue()) 41 case ldvalue.NumberType: 42 unixMillis := int64(value.Float64Value()) 43 return time.Unix(0, unixMillis*int64(time.Millisecond)).UTC(), true 44 } 45 return time.Time{}, false 46 } 47 48 // ValueToSemanticVersion attempts to convert a JSON value to a semver.Version. 49 // 50 // If the value is a string, it is parsed with the parser defined in the semver package. Any 51 // other type is invalid. 52 // 53 // The second return value is true for success or false for failure. 54 func (e TypeConversionMethods) ValueToSemanticVersion(value ldvalue.Value) (semver.Version, bool) { 55 return parseSemVer(value) 56 } 57