Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove human friendly digit group separators #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zleinweber
Copy link

@zleinweber zleinweber commented Feb 10, 2022

Some APIs that return numeric values as a string that include commas or underscores to separate digit groups. For example, "1,000". This causes an error in SanitizeValue when the string gets passed into strconv.ParseFloat().

This fixes the problem above transparently.

@zleinweber zleinweber force-pushed the sanitize_digit_separators branch from e392410 to c55a252 Compare February 10, 2022 20:47
@@ -34,6 +35,8 @@ import (
pconfig "github.com/prometheus/common/config"
)

var sanitzieValueRE = regexp.MustCompile(`(,|_)`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than use a regexp, how about a string replacer?

Suggested change
var sanitzieValueRE = regexp.MustCompile(`(,|_)`)
var sanitzieValue = strings.NewReplacer(",", "", "|", "")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- var sanitzieValue = strings.NewReplacer(",", "", "|", "")
+ var sanitzieValue = strings.NewReplacer(",", "", "_", "")

@@ -43,7 +46,7 @@ func SanitizeValue(s string) (float64, error) {
var value float64
var resultErr string

if value, err = strconv.ParseFloat(s, 64); err == nil {
if value, err = strconv.ParseFloat(sanitzieValueRE.ReplaceAllString(s, ""), 64); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if value, err = strconv.ParseFloat(sanitzieValueRE.ReplaceAllString(s, ""), 64); err == nil {
if value, err = strconv.ParseFloat(sanitzieValue.Replace(s), 64); err == nil {

@SuperQ SuperQ requested a review from rustycl0ck July 3, 2022 08:52
Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use strings instead of regexp.

@SuperQ
Copy link
Contributor

SuperQ commented Jun 6, 2023

Ping, please resolve requested changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants