stringToRat is a helper function that tries to convert a string to a rational number while allowing percentage, decimal, and fraction values.
(s string)
| 71 | // stringToRat is a helper function that tries to convert a string to a rational |
| 72 | // number while allowing percentage, decimal, and fraction values. |
| 73 | func stringToRat(s string) (*big.Rat, error) { |
| 74 | if before, ok := strings.CutSuffix(s, "%"); ok { |
| 75 | num, ok := new(big.Int).SetString(before, 10) |
| 76 | if !ok { |
| 77 | return nil, fmt.Errorf("'%s' is not a valid percentage", s) |
| 78 | } |
| 79 | return new(big.Rat).SetFrac(num, big.NewInt(100)), nil |
| 80 | } |
| 81 | rat, ok := new(big.Rat).SetString(s) |
| 82 | if !ok { |
| 83 | return nil, fmt.Errorf("'%s' is not a valid percentage, decimal, fraction or interval value", s) |
| 84 | } |
| 85 | return rat, nil |
| 86 | } |
| 87 | |
| 88 | // NewExecutionSegmentFromString validates the supplied string value and returns |
| 89 | // the newly created ExecutionSegment or and error from it. |
no test coverage detected
searching dependent graphs…