Skip to content

Commit

Permalink
refactor: append* funcs now follow builtin append convention
Browse files Browse the repository at this point in the history
  • Loading branch information
wazazaby committed Mar 22, 2024
1 parent 3a89e29 commit 9bc6f27
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
41 changes: 41 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package vimebu

import "strconv"

// appendStringValue quotes (if needed) and appends s to dst and returns the extended buffer.
func appendStringValue(dst []byte, s string, escapeQuote bool) []byte {
if escapeQuote {
return strconv.AppendQuote(dst, s)
}
dst = append(dst, doubleQuotesByte)
dst = append(dst, s...)
return append(dst, doubleQuotesByte)
}

// appendBoolValue appends b to dst and returns the extended buffer.
func appendBoolValue(dst []byte, b bool) []byte {
dst = append(dst, doubleQuotesByte)
dst = strconv.AppendBool(dst, b)
return append(dst, doubleQuotesByte)
}

// appendInt64Value appends i to dst and returns the extended buffer.
func appendInt64Value(dst []byte, i int64) []byte {
dst = append(dst, doubleQuotesByte)
dst = strconv.AppendInt(dst, i, 10)
return append(dst, doubleQuotesByte)
}

// appendUint64Value appends i to dst and returns the extended buffer.
func appendUint64Value(dst []byte, i uint64) []byte {
dst = append(dst, doubleQuotesByte)
dst = strconv.AppendUint(dst, i, 10)
return append(dst, doubleQuotesByte)
}

// appendFloat64Value appends f to dst and returns the extended buffer.
func appendFloat64Value(dst []byte, f float64) []byte {
dst = append(dst, doubleQuotesByte)
dst = strconv.AppendFloat(dst, f, 'f', -1, 64)
return append(dst, doubleQuotesByte)
}
62 changes: 7 additions & 55 deletions vimebu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"log"
"strconv"
"strings"
"unsafe"
)
Expand Down Expand Up @@ -289,71 +288,24 @@ func (b *Builder) label(name string, escapeQuote bool, stringValue *string, bool

b.underlying.WriteString(name)
b.underlying.WriteByte(equalByte)
buf := b.underlying.AvailableBuffer()
switch {
case stringValue != nil:
b.appendString(*stringValue, escapeQuote)
buf = appendStringValue(buf, *stringValue, escapeQuote)
case boolValue != nil:
b.appendBool(*boolValue)
buf = appendBoolValue(buf, *boolValue)
case uint64Value != nil:
b.appendUint64(*uint64Value)
buf = appendUint64Value(buf, *uint64Value)
case int64Value != nil:
b.appendInt64(*int64Value)
buf = appendInt64Value(buf, *int64Value)
case float64Value != nil:
b.appendFloat64(*float64Value)
buf = appendFloat64Value(buf, *float64Value)
default: // Internal problem (wrong use of the label function), panic.
panic("unsupported case - no label value set")
}

return b
}

// appendString quotes (if needed) and appends s to b's underlying buffer.
func (b *Builder) appendString(s string, escapeQuote bool) {
buf := b.underlying.AvailableBuffer()
if escapeQuote {
buf = strconv.AppendQuote(buf, s)
} else {
buf = append(buf, doubleQuotesByte)
buf = append(buf, s...)
buf = append(buf, doubleQuotesByte)
}
b.underlying.Write(buf)
}

// appendBool appends bl to b's underlying buffer.
func (b *Builder) appendBool(bl bool) {
buf := b.underlying.AvailableBuffer()
buf = append(buf, doubleQuotesByte)
buf = strconv.AppendBool(buf, bl)
buf = append(buf, doubleQuotesByte)
b.underlying.Write(buf)
}

// appendInt64 appends i to b's underlying buffer.
func (b *Builder) appendInt64(i int64) {
buf := b.underlying.AvailableBuffer()
buf = append(buf, doubleQuotesByte)
buf = strconv.AppendInt(buf, i, 10)
buf = append(buf, doubleQuotesByte)
b.underlying.Write(buf)
}

// appendUint64 appends i to b's underlying buffer.
func (b *Builder) appendUint64(i uint64) {
buf := b.underlying.AvailableBuffer()
buf = append(buf, doubleQuotesByte)
buf = strconv.AppendUint(buf, i, 10)
buf = append(buf, doubleQuotesByte)
b.underlying.Write(buf)
}

// appendFloat64 appends f to b's underlying buffer.
func (b *Builder) appendFloat64(f float64) {
buf := b.underlying.AvailableBuffer()
buf = append(buf, doubleQuotesByte)
buf = strconv.AppendFloat(buf, f, 'f', -1, 64)
buf = append(buf, doubleQuotesByte)
b.underlying.Write(buf)
return b
}

// String builds the metric by returning the accumulated string.
Expand Down

0 comments on commit 9bc6f27

Please sign in to comment.