-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.go
More file actions
123 lines (110 loc) · 3.87 KB
/
query.go
File metadata and controls
123 lines (110 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2020–2026 The query developers. All rights reserved.
// Project site: https://github.com/gotmc/query
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
// Package query provides convenience functions for parsing the string results
// from a Querier interface into Go types such as bool, int, float64, and
// string.
package query
import (
"context"
"fmt"
"math"
"strconv"
"strings"
)
// Querier provides the interface to query using a given command and provide
// the resultant string. The command string should include the appropriate
// terminator for the instrument.
type Querier interface {
Query(ctx context.Context, cmd string) (string, error)
}
// TruncationError is returned by Int when a float value is truncated to an
// integer. The truncated value is still returned alongside this error.
type TruncationError struct {
Value int
Original string
}
func (e *TruncationError) Error() string {
return fmt.Sprintf("value %s truncated to %d", e.Original, e.Value)
}
// Bool queries a Querier with the given command and returns a bool.
func Bool(ctx context.Context, q Querier, cmd string) (bool, error) {
s, err := q.Query(ctx, cmd)
if err != nil {
return false, fmt.Errorf("querying bool %q: %w", cmd, err)
}
switch strings.ToUpper(strings.TrimSpace(s)) {
case "OFF", "0", "FALSE":
return false, nil
case "ON", "1", "TRUE":
return true, nil
default:
return false, fmt.Errorf("could not determine boolean status from %q", s)
}
}
// Boolf queries the Querier according to a format specifier and returns a
// bool.
func Boolf(ctx context.Context, q Querier, format string, a ...any) (bool, error) {
return Bool(ctx, q, fmt.Sprintf(format, a...))
}
// Float64 queries the Querier with the given command and returns a float64.
func Float64(ctx context.Context, q Querier, cmd string) (float64, error) {
s, err := q.Query(ctx, cmd)
if err != nil {
return 0, fmt.Errorf("querying float64 %q: %w", cmd, err)
}
f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if err != nil {
return 0, fmt.Errorf("parsing float64 for %q: %w", cmd, err)
}
return f, nil
}
// Float64f queries the querier according to a format specifier and returns a
// float.
func Float64f(ctx context.Context, q Querier, format string, a ...any) (float64, error) {
return Float64(ctx, q, fmt.Sprintf(format, a...))
}
// Int queries the querier with the given command and returns an int.
func Int(ctx context.Context, q Querier, cmd string) (int, error) {
s, err := q.Query(ctx, cmd)
if err != nil {
return 0, fmt.Errorf("querying int %q: %w", cmd, err)
}
s = strings.TrimSpace(s)
i, err := strconv.Atoi(s)
if err != nil {
// The string might be in scientific notation (e.g., "+1.23E+02").
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, fmt.Errorf("parsing int for %q: %w", cmd, err)
}
if f > float64(math.MaxInt) || f < float64(math.MinInt) {
return 0, fmt.Errorf("value %s overflows int for %q", s, cmd)
}
if f != math.Trunc(f) {
return int(f), &TruncationError{Value: int(f), Original: s}
}
return int(f), nil
}
return i, nil
}
// Intf queries the querier according to a format specifier and returns a
// int.
func Intf(ctx context.Context, q Querier, format string, a ...any) (int, error) {
return Int(ctx, q, fmt.Sprintf(format, a...))
}
// String queries the querier with the given command and returns a string
// trimming any whitespace.
func String(ctx context.Context, q Querier, cmd string) (string, error) {
s, err := q.Query(ctx, cmd)
if err != nil {
return "", fmt.Errorf("querying string %q: %w", cmd, err)
}
return strings.TrimSpace(s), nil
}
// Stringf queries the querier according to a format specifier and returns a
// string.
func Stringf(ctx context.Context, q Querier, format string, a ...any) (string, error) {
return String(ctx, q, fmt.Sprintf(format, a...))
}