Skip to content

Commit

Permalink
Merge pull request #178 from prometheus/mdl-powersupply-noreflect
Browse files Browse the repository at this point in the history
sysfs: rewrite PowerSupply parsing code without reflect
  • Loading branch information
mdlayher authored Jun 12, 2019
2 parents 1de2bc5 + 09a0b2e commit 9fb8535
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 137 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module github.com/prometheus/procfs

require golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
require (
github.com/google/go-cmp v0.3.0
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
58 changes: 58 additions & 0 deletions internal/util/valueparser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"strconv"
)

// TODO(mdlayher): util packages are an anti-pattern and this should be moved
// somewhere else that is more focused in the future.

// A ValueParser enables parsing a single string into a variety of data types
// in a concise and safe way. The Err method must be invoked after invoking
// any other methods to ensure a value was successfully parsed.
type ValueParser struct {
v string
err error
}

// NewValueParser creates a ValueParser using the input string.
func NewValueParser(v string) *ValueParser {
return &ValueParser{v: v}
}

// PInt64 interprets the underlying value as an int64 and returns a pointer to
// that value.
func (vp *ValueParser) PInt64() *int64 {
if vp.err != nil {
return nil
}

// A base value of zero makes ParseInt infer the correct base using the
// string's prefix, if any.
const base = 0
v, err := strconv.ParseInt(vp.v, base, 64)
if err != nil {
vp.err = err
return nil
}

return &v
}

// Err returns the last error, if any, encountered by the ValueParser.
func (vp *ValueParser) Err() error {
return vp.err
}
92 changes: 92 additions & 0 deletions internal/util/valueparser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/prometheus/procfs/internal/util"
)

func TestValueParser(t *testing.T) {
tests := []struct {
name string
v string
ok bool
fn func(t *testing.T, vp *util.ValueParser)
}{
{
name: "bad PInt64",
v: "hello",
fn: func(_ *testing.T, vp *util.ValueParser) {
_ = vp.PInt64()
},
},
{
name: "bad hex PInt64",
v: "0xhello",
fn: func(_ *testing.T, vp *util.ValueParser) {
_ = vp.PInt64()
},
},
{
name: "ok PInt64",
v: "1",
ok: true,
fn: func(t *testing.T, vp *util.ValueParser) {
want := int64(1)
got := vp.PInt64()

if diff := cmp.Diff(&want, got); diff != "" {
t.Fatalf("unexpected integer (-want +got):\n%s", diff)
}
},
},
{
name: "ok hex PInt64",
v: "0xff",
ok: true,
fn: func(t *testing.T, vp *util.ValueParser) {
want := int64(255)
got := vp.PInt64()

if diff := cmp.Diff(&want, got); diff != "" {
t.Fatalf("unexpected integer (-want +got):\n%s", diff)
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vp := util.NewValueParser(tt.v)
tt.fn(t, vp)

err := vp.Err()
if err != nil {
if tt.ok {
t.Fatalf("unexpected error: %v", err)
}

t.Logf("OK err: %v", err)
return
}

if !tt.ok {
t.Fatal("expected an error, but none occurred")
}
})
}
}
Loading

0 comments on commit 9fb8535

Please sign in to comment.