Skip to content

Commit

Permalink
update builderFunc godoc, make naming more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
wazazaby committed Oct 8, 2023
1 parent 74d6567 commit b744fe9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
24 changes: 19 additions & 5 deletions vimebu_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"strings"
)

// LabelCallback TODO:
type LabelCallback func() (name, value string, escapeQuote bool)

// WithLabel TODO:
// WithLabel appends a pair of label name and label value to the builder.
// Unlike [vimebu.WithLabelQuote], quotes inside the label value will not be escaped.
// It's better suited for a label value where you control the input (either it is already sanitized, or it comes from a const or an enum for example).
//
// Panics if the label name or label value contain more than [vimebu.LabelNameMaxLen] or [vimebu.LabelValueMaxLen] bytes respectively.
func WithLabel(name, value string) LabelCallback {
if len(name) > LabelNameMaxLen {
panic("label name contains too many bytes")
Expand All @@ -23,15 +26,20 @@ func WithLabel(name, value string) LabelCallback {
}
}

// WithLabelCond TODO:
// WithLabelCond is a wrapper around [vimebu.WithLabel].
// It allows you to conditionally add a label depending on the output of a predicate function.
//
// Panics if the label name or label value contain more than [vimebu.LabelNameMaxLen] or [vimebu.LabelValueMaxLen] bytes respectively.
func WithLabelCond(fn func() bool, name, value string) LabelCallback {
if fn() {
return WithLabel(name, value)
}
return WithLabel("", "")
}

// WithLabelQuote TODO:
// WithLabelQuote appends a pair of label name and label value to the builder. Quotes inside the label value will be escaped.
//
// Panics if the label name or label value contain more than [vimebu.LabelNameMaxLen] or [vimebu.LabelValueMaxLen] bytes respectively.
func WithLabelQuote(name, value string) LabelCallback {
if len(name) > LabelNameMaxLen {
panic("label name contains too many bytes")
Expand All @@ -46,7 +54,10 @@ func WithLabelQuote(name, value string) LabelCallback {
}
}

// WithLabelQuoteCond TODO:
// WithLabelQuoteCond is a wrapper around [vimebu.WithLabelQuote].
// It allows you to conditionally add a label depending on the output of a predicate function.
//
// Panics if the label name or label value contain more than [vimebu.LabelNameMaxLen] or [vimebu.LabelValueMaxLen] bytes respectively.
func WithLabelQuoteCond(fn func() bool, name, value string) LabelCallback {
if fn() {
return WithLabelQuote(name, value)
Expand All @@ -55,6 +66,9 @@ func WithLabelQuoteCond(fn func() bool, name, value string) LabelCallback {
}

// BuilderFunc is used to efficiently build a VictoriaMetrics metric.
// It's backed by a bytes.Buffer to minimize memory copying.
//
// Panics if the name is empty, contains more than [vimebu.MetricNameMaxLen] bytes or if it contains a double quote.
func BuilderFunc(name string, labels ...LabelCallback) string {
ln := len(name)
if ln == 0 {
Expand Down
10 changes: 5 additions & 5 deletions vimebu_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/require"
)

func handleTestCaseFn(t *testing.T, tc testCase) {
func handleTestCaseFunc(t *testing.T, tc testCase) {
labels := make([]LabelCallback, 0, len(tc.input.labels))

for _, label := range tc.input.labels {
Expand All @@ -21,16 +21,16 @@ func handleTestCaseFn(t *testing.T, tc testCase) {
require.Equal(t, tc.expected, result)
}

func TestBuilderFn(t *testing.T) {
func TestBuilderFunc(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.mustPanic {
require.Panics(t, func() {
handleTestCaseFn(t, tc)
handleTestCaseFunc(t, tc)
})
} else {
require.NotPanics(t, func() {
handleTestCaseFn(t, tc)
handleTestCaseFunc(t, tc)
})
}
})
Expand All @@ -47,7 +47,7 @@ func TestLabelCond(t *testing.T) {
require.Equal(t, `test_cond{should="appear",present="/and/\"quoted\""}`, result)
}

func BenchmarkBuilderFnTestCases(b *testing.B) {
func BenchmarkBuilderFuncTestCases(b *testing.B) {
for _, tc := range testCases {
if tc.mustPanic { // Skip test cases that panics as they will break the benchmarks.
continue
Expand Down

0 comments on commit b744fe9

Please sign in to comment.