Skip to content

Commit

Permalink
refactor: consolidate protohelpers into root package
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Stewart <[email protected]>
  • Loading branch information
paralin committed Apr 19, 2024
1 parent 008f344 commit 71b78a0
Show file tree
Hide file tree
Showing 18 changed files with 3,081 additions and 3,084 deletions.
2 changes: 1 addition & 1 deletion features/marshal/marshalto.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func (p *marshal) field(oneof bool, numGen *counter, field *protogen.Field) {
// See https://github.com/planetscale/vtprotobuf/issues/61
if oneof && field.Desc.Kind() == protoreflect.MessageKind && !field.Desc.IsMap() && !field.Desc.IsList() {
p.P("} else {")
p.P("i = protohelpers.EncodeVarint(dAtA, i, 0)")
p.P("i = ", p.Helper("EncodeVarint"), "(dAtA, i, 0)")
p.encodeKey(fieldNumber, wireType)
p.P("}")
} else if repeated || nullable {
Expand Down
2 changes: 1 addition & 1 deletion generator/generatedfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *GeneratedFile) IsLocalField(field *protogen.Field) bool {
return p.LocalPackages[pkg]
}

const vtHelpersPackage = protogen.GoImportPath("github.com/aperturerobotics/protobuf-go-lite/protohelpers")
const vtHelpersPackage = protogen.GoImportPath("github.com/aperturerobotics/protobuf-go-lite")

var helpers = map[string]protogen.GoIdent{
"EncodeVarint": {GoName: "EncodeVarint", GoImportPath: vtHelpersPackage},
Expand Down
120 changes: 120 additions & 0 deletions protobuf-go-lite.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package protobuf_go_lite

import (
"io"
"math/bits"

"github.com/pkg/errors"
)

var (
// ErrInvalidLength is returned when decoding a negative length.
ErrInvalidLength = errors.New("proto: negative length found during unmarshaling")
// ErrIntOverflow is returned when decoding a varint representation of an integer that overflows 64 bits.
ErrIntOverflow = errors.New("proto: integer overflow")
// ErrUnexpectedEndOfGroup is returned when decoding a group end without a corresponding group start.
ErrUnexpectedEndOfGroup = errors.New("proto: unexpected end of group")
)

// Message is the base vtprotobuf message marshal/unmarshal interface.
type Message interface {
MarshalVT() ([]byte, error)
Expand Down Expand Up @@ -40,3 +56,107 @@ func IsEqualVT[T EqualVT[T]](t1, t2 T) bool {
}
return t1.EqualVT(t2)
}

// EncodeVarint encodes a uint64 into a varint-encoded byte slice and returns the offset of the encoded value.
// The provided offset is the offset after the last byte of the encoded value.
func EncodeVarint(dAtA []byte, offset int, v uint64) int {
offset -= SizeOfVarint(v)
base := offset
for v >= 1<<7 {
dAtA[offset] = uint8(v&0x7f | 0x80)
v >>= 7
offset++
}
dAtA[offset] = uint8(v)
return base
}

// SizeOfVarint returns the size of the varint-encoded value.
func SizeOfVarint(x uint64) (n int) {
return (bits.Len64(x|1) + 6) / 7
}

// SizeOfZigzag returns the size of the zigzag-encoded value.
func SizeOfZigzag(x uint64) (n int) {
return SizeOfVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) //nolint
}

// Skip the first record of the byte slice and return the offset of the next record.
func Skip(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0
depth := 0
for iNdEx < l {
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflow
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
wireType := int(wire & 0x7)
switch wireType {
case 0:
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflow
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
iNdEx++
if dAtA[iNdEx-1] < 0x80 {
break
}
}
case 1:
iNdEx += 8
case 2:
var length int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflow
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
length |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
if length < 0 {
return 0, ErrInvalidLength
}
iNdEx += length
case 3:
depth++
case 4:
if depth == 0 {
return 0, ErrUnexpectedEndOfGroup
}
depth--
case 5:
iNdEx += 4
default:
return 0, errors.Errorf("proto: illegal wireType %d", wireType)
}
if iNdEx < 0 {
return 0, ErrInvalidLength
}
if depth == 0 {
return iNdEx, nil
}
}
return 0, io.ErrUnexpectedEOF
}
123 changes: 0 additions & 123 deletions protohelpers/protohelpers.go

This file was deleted.

Loading

0 comments on commit 71b78a0

Please sign in to comment.