lib/prompbmarshal: code cleanup after 8aaa828ba3

This commit is contained in:
Aliaksandr Valialkin 2024-02-01 21:31:08 +02:00
parent 0e3c532bf7
commit a40fcc8aa6
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 64 additions and 104 deletions

View file

@ -4,61 +4,50 @@
package prompbmarshal package prompbmarshal
import ( import (
math_bits "math/bits" "math/bits"
) )
type WriteRequest struct { type WriteRequest struct {
Timeseries []TimeSeries Timeseries []TimeSeries
} }
func (m *WriteRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { func (m *WriteRequest) MarshalToSizedBuffer(dst []byte) (int, error) {
i := len(dAtA) i := len(dst)
_ = i for j := len(m.Timeseries) - 1; j >= 0; j-- {
var l int size, err := m.Timeseries[j].MarshalToSizedBuffer(dst[:i])
_ = l if err != nil {
if len(m.Timeseries) > 0 { return 0, err
for iNdEx := len(m.Timeseries) - 1; iNdEx >= 0; iNdEx-- {
{
size, err := m.Timeseries[iNdEx].MarshalToSizedBuffer(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = encodeVarint(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0xa
} }
i -= size
i = encodeVarint(dst, i, uint64(size))
i--
dst[i] = 0xa
} }
return len(dAtA) - i, nil return len(dst) - i, nil
} }
func encodeVarint(dAtA []byte, offset int, v uint64) int { func encodeVarint(dst []byte, offset int, v uint64) int {
offset -= sov(v) offset -= sov(v)
base := offset base := offset
for v >= 1<<7 { for v >= 1<<7 {
dAtA[offset] = uint8(v&0x7f | 0x80) dst[offset] = uint8(v&0x7f | 0x80)
v >>= 7 v >>= 7
offset++ offset++
} }
dAtA[offset] = uint8(v) dst[offset] = uint8(v)
return base return base
} }
func (m *WriteRequest) Size() (n int) { func (m *WriteRequest) Size() (n int) {
if m == nil { if m == nil {
return 0 return 0
} }
var l int for _, e := range m.Timeseries {
_ = l l := e.Size()
if len(m.Timeseries) > 0 { n += 1 + l + sov(uint64(l))
for _, e := range m.Timeseries {
l = e.Size()
n += 1 + l + sov(uint64(l))
}
} }
return n return n
} }
func sov(x uint64) (n int) { func sov(x uint64) (n int) {
return (math_bits.Len64(x|1) + 6) / 7 return (bits.Len64(x|1) + 6) / 7
} }

View file

@ -4,8 +4,8 @@
package prompbmarshal package prompbmarshal
import ( import (
encoding_binary "encoding/binary" "encoding/binary"
math "math" "math"
) )
type Sample struct { type Sample struct {
@ -24,89 +24,70 @@ type Label struct {
Value string Value string
} }
func (m *Sample) MarshalToSizedBuffer(dAtA []byte) (int, error) { func (m *Sample) MarshalToSizedBuffer(dst []byte) (int, error) {
i := len(dAtA) i := len(dst)
_ = i
var l int
_ = l
if m.Timestamp != 0 { if m.Timestamp != 0 {
i = encodeVarint(dAtA, i, uint64(m.Timestamp)) i = encodeVarint(dst, i, uint64(m.Timestamp))
i-- i--
dAtA[i] = 0x10 dst[i] = 0x10
} }
if m.Value != 0 { if m.Value != 0 {
i -= 8 i -= 8
encoding_binary.LittleEndian.PutUint64(dAtA[i:], uint64(math.Float64bits(float64(m.Value)))) binary.LittleEndian.PutUint64(dst[i:], uint64(math.Float64bits(float64(m.Value))))
i-- i--
dAtA[i] = 0x9 dst[i] = 0x9
} }
return len(dAtA) - i, nil return len(dst) - i, nil
} }
func (m *TimeSeries) MarshalToSizedBuffer(dAtA []byte) (int, error) { func (m *TimeSeries) MarshalToSizedBuffer(dst []byte) (int, error) {
i := len(dAtA) i := len(dst)
_ = i for j := len(m.Samples) - 1; j >= 0; j-- {
var l int size, err := m.Samples[j].MarshalToSizedBuffer(dst[:i])
_ = l if err != nil {
if len(m.Samples) > 0 { return 0, err
for iNdEx := len(m.Samples) - 1; iNdEx >= 0; iNdEx-- {
{
size, err := m.Samples[iNdEx].MarshalToSizedBuffer(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = encodeVarint(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0x12
} }
i -= size
i = encodeVarint(dst, i, uint64(size))
i--
dst[i] = 0x12
} }
if len(m.Labels) > 0 { for j := len(m.Labels) - 1; j >= 0; j-- {
for iNdEx := len(m.Labels) - 1; iNdEx >= 0; iNdEx-- { size, err := m.Labels[j].MarshalToSizedBuffer(dst[:i])
{ if err != nil {
size, err := m.Labels[iNdEx].MarshalToSizedBuffer(dAtA[:i]) return 0, err
if err != nil {
return 0, err
}
i -= size
i = encodeVarint(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0xa
} }
i -= size
i = encodeVarint(dst, i, uint64(size))
i--
dst[i] = 0xa
} }
return len(dAtA) - i, nil return len(dst) - i, nil
} }
func (m *Label) MarshalToSizedBuffer(dAtA []byte) (int, error) { func (m *Label) MarshalToSizedBuffer(dst []byte) (int, error) {
i := len(dAtA) i := len(dst)
_ = i
var l int
_ = l
if len(m.Value) > 0 { if len(m.Value) > 0 {
i -= len(m.Value) i -= len(m.Value)
copy(dAtA[i:], m.Value) copy(dst[i:], m.Value)
i = encodeVarint(dAtA, i, uint64(len(m.Value))) i = encodeVarint(dst, i, uint64(len(m.Value)))
i-- i--
dAtA[i] = 0x12 dst[i] = 0x12
} }
if len(m.Name) > 0 { if len(m.Name) > 0 {
i -= len(m.Name) i -= len(m.Name)
copy(dAtA[i:], m.Name) copy(dst[i:], m.Name)
i = encodeVarint(dAtA, i, uint64(len(m.Name))) i = encodeVarint(dst, i, uint64(len(m.Name)))
i-- i--
dAtA[i] = 0xa dst[i] = 0xa
} }
return len(dAtA) - i, nil return len(dst) - i, nil
} }
func (m *Sample) Size() (n int) { func (m *Sample) Size() (n int) {
if m == nil { if m == nil {
return 0 return 0
} }
var l int
_ = l
if m.Value != 0 { if m.Value != 0 {
n += 9 n += 9
} }
@ -120,19 +101,13 @@ func (m *TimeSeries) Size() (n int) {
if m == nil { if m == nil {
return 0 return 0
} }
var l int for _, e := range m.Labels {
_ = l l := e.Size()
if len(m.Labels) > 0 { n += 1 + l + sov(uint64(l))
for _, e := range m.Labels {
l = e.Size()
n += 1 + l + sov(uint64(l))
}
} }
if len(m.Samples) > 0 { for _, e := range m.Samples {
for _, e := range m.Samples { l := e.Size()
l = e.Size() n += 1 + l + sov(uint64(l))
n += 1 + l + sov(uint64(l))
}
} }
return n return n
} }
@ -141,14 +116,10 @@ func (m *Label) Size() (n int) {
if m == nil { if m == nil {
return 0 return 0
} }
var l int if l := len(m.Name); l > 0 {
_ = l
l = len(m.Name)
if l > 0 {
n += 1 + l + sov(uint64(l)) n += 1 + l + sov(uint64(l))
} }
l = len(m.Value) if l := len(m.Value); l > 0 {
if l > 0 {
n += 1 + l + sov(uint64(l)) n += 1 + l + sov(uint64(l))
} }
return n return n