Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 472a556

Browse files
authored
msp/runtime: add GetFloat to runtime Env (#63399)
The runtime `Env` currently lacks a convenient method for getting a floating point env var. This PR adds a `GetFloat`method which will parse a float64 from a given env var Part of CORE-181 ## Test plan locally tested values such as `10.2` and `infinity` were correctly parsed
1 parent 18f42a7 commit 472a556

File tree

1 file changed

+17
-0
lines changed
  • lib/managedservicesplatform/runtime/contract

1 file changed

+17
-0
lines changed

lib/managedservicesplatform/runtime/contract/env.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ func (e *Env) GetBool(name, defaultValue, description string) bool {
215215
return v
216216
}
217217

218+
// GetFloat returns the value with the given name interpreted as a float64. If no
219+
// value was supplied in the environment, the given default is used in its place.
220+
// If no value is available, or if the given value or default cannot be converted
221+
// to a float64, an error is added to the validation errors list.
222+
//
223+
// The name, defaultValue, and description are reported when running a service
224+
// using the MSP runtime with the '-help' flag.
225+
func (e *Env) GetFloat(name, defaultValue, description string) float64 {
226+
rawValue := e.get(name, defaultValue, description)
227+
v, err := strconv.ParseFloat(rawValue, 64)
228+
if err != nil {
229+
e.AddError(errors.Errorf("invalid float %q for %s: %s", rawValue, name, err))
230+
return 0
231+
}
232+
return v
233+
}
234+
218235
// AddError adds a validation error to the configuration object. This should be
219236
// called from within the Load method of a decorated configuration object to have
220237
// any effect. The error is silently collected and can be retrieved using

0 commit comments

Comments
 (0)