Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using of the correct name for oneof message and options #124

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dist/

# editor-related files
.vscode/
.idea

# Go workspace files
go.work*
9 changes: 3 additions & 6 deletions cmd/protoc-gen-fastmarshal/templates/fieldsnippets.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,10 @@
{{ end }}
{{/* SizeOfOneOf - calculate the size of a oneof field */}}
{{ define "SizeOfOneOf" }}
{{- $msgDef := .Parent -}}
// {{.GoName | getSafeFieldName}} (oneof)
if m.{{.GoName | getSafeFieldName}} != nil {
switch typedVal := m.{{.GoName | getSafeFieldName}}.(type) {
{{ range .Fields }}case *{{ $msgDef.Desc.Name }}_{{.GoName | getSafeFieldName}}:
{{ range .Fields }}case *{{.GoIdent.GoName | getSafeFieldName}}:
{{- $kind := (.Desc.Kind | string) -}}
// {{.Desc.Name}} ({{.Desc.Number}},{{.Desc.Kind}})
{{ if eq $kind "bool" -}}
Expand Down Expand Up @@ -615,10 +614,9 @@
{{ end }}
{{/* MarshalOneOf - generates the snippet to marshal a oneof field */}}
{{ define "MarshalOneOf" }}
{{- $msgDef := .Parent -}}
if m.{{.GoName | getSafeFieldName}} != nil {
switch typedVal := m.{{.GoName | getSafeFieldName}}.(type) {
{{ range .Fields }}case *{{ $msgDef.Desc.Name }}_{{.GoName | getSafeFieldName}}: // {{.Desc.Name}} ({{.Desc.Number}},{{.Desc.Kind}})
{{ range .Fields }}case *{{.GoIdent.GoName | getSafeFieldName}}: // {{.Desc.Name}} ({{.Desc.Number}},{{.Desc.Kind}})
{{- $kind := (.Desc.Kind | string) -}}
{{ if eq $kind "bool" "int32" "int64" "uint32" "uint64" "sint32" "sint64" "fixed32" "float" "fixed64" "double" }}
enc.{{ protoNumberEncodeMethod (.Desc.Kind | string) false}}({{.Desc.Number}}, typedVal.{{.GoName | getSafeFieldName}})
Expand Down Expand Up @@ -1230,12 +1228,11 @@
{{ end }}
{{/* UnmarshalOneOf - generates the snippet to unmarshal a oneof field */}}
{{ define "UnmarshalOneOf" }}
{{- $msgDef := .Parent -}}
{{- $oneOf := . -}}
{{- range .Fields -}}
{{- $kind := (.Desc.Kind | string) -}}
case {{.Desc.Number}}: // {{$oneOf.Desc.Name}}.{{.Desc.Name}} (oneof,{{.Desc.Kind}})
var ov {{$msgDef.Desc.Name}}_{{.GoName | getSafeFieldName}}
var ov {{.GoIdent.GoName | getSafeFieldName}}
{{ if eq $kind "bool" -}}
if wt != csproto.WireTypeVarint {
return fmt.Errorf("incorrect wire type %v for tag field '{{.Desc.Name}}' (tag={{.Desc.Number}}), expected 0 (varint)", wt)
Expand Down
1 change: 0 additions & 1 deletion cmd/protoc-gen-fastmarshal/templates/singlefile.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

{{ range allMessages }}
{{- $msgDef := . -}}

//------------------------------------------------------------------------------
// Custom Protobuf size/marshal/unmarshal code for {{ .GoIdent.GoName }}
Expand Down
362 changes: 277 additions & 85 deletions example/permessage/gogo/gogo_permessage_example.pb.go

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions example/permessage/gogo/gogo_permessage_example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,21 @@ message AllTheMaps {
map<string, EmbeddedEvent> toMessage = 15;
map<string, EventType> toEnum = 16;
}

// see https://github.com/CrowdStrike/csproto/pull/124
message I18nVariable {
oneof one_of_values {
string opt_one = 1;
string opt_two = 2;
}
}

message Msg {
oneof one_of_values {
Msg.Tags tags = 1;
}

message Tags {
repeated string tags = 1;
}
}
138 changes: 138 additions & 0 deletions example/permessage/gogo/gogo_permessage_example_i18nvariable.pb.fm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// GENERATED CODE - DO NOT EDIT
// This file was generated by protoc-gen-fastmarshal

package gogo

import (
"fmt"
"sync/atomic"
"github.com/CrowdStrike/csproto"
)

//------------------------------------------------------------------------------
// Custom Protobuf size/marshal/unmarshal code for I18NVariable

// Size calculates and returns the size, in bytes, required to hold the contents of m using the Protobuf
// binary encoding.
func (m *I18NVariable) Size() int {
// nil message is always 0 bytes
if m == nil {
return 0
}
// return cached size, if present
if csz := int(atomic.LoadInt32(&m.XXX_sizecache)); csz > 0 {
return csz
}
// calculate and cache
var sz, l int
_ = l // avoid unused variable

// OneOfValues (oneof)
if m.OneOfValues != nil {
switch typedVal := m.OneOfValues.(type) {
case *I18NVariable_OptOne: // opt_one (1,string)
l = len(typedVal.OptOne)
sz += csproto.SizeOfTagKey(1) + csproto.SizeOfVarint(uint64(l)) + l
case *I18NVariable_OptTwo: // opt_two (2,string)
l = len(typedVal.OptTwo)
sz += csproto.SizeOfTagKey(2) + csproto.SizeOfVarint(uint64(l)) + l
default:
_ = typedVal // ensure no unused variable
}
}

// cache the size so it can be re-used in Marshal()/MarshalTo()
atomic.StoreInt32(&m.XXX_sizecache, int32(sz))
return sz
}

// Marshal converts the contents of m to the Protobuf binary encoding and returns the result or an error.
func (m *I18NVariable) Marshal() ([]byte, error) {
siz := m.Size()
if siz == 0 {
return []byte{}, nil
}
buf := make([]byte, siz)
err := m.MarshalTo(buf)
return buf, err
}

// MarshalTo converts the contents of m to the Protobuf binary encoding and writes the result to dest.
func (m *I18NVariable) MarshalTo(dest []byte) error {
// nil message == no-op
if m == nil {
return nil
}
var (
enc = csproto.NewEncoder(dest)
buf []byte
err error
extVal interface{}
)
// ensure no unused variables
_ = enc
_ = buf
_ = err
_ = extVal

// OneOfValues (oneof)

if m.OneOfValues != nil {
switch typedVal := m.OneOfValues.(type) {
case *I18NVariable_OptOne: // opt_one (1,string)
enc.EncodeString(1, typedVal.OptOne)
case *I18NVariable_OptTwo: // opt_two (2,string)
enc.EncodeString(2, typedVal.OptTwo)
default:
_ = typedVal // ensure no unused variable
}
}
return nil
}

// Unmarshal decodes a binary encoded Protobuf message from p and populates m with the result.
func (m *I18NVariable) Unmarshal(p []byte) error {
m.Reset()
if len(p) == 0 {
return nil
}
dec := csproto.NewDecoder(p)
for dec.More() {
tag, wt, err := dec.DecodeTag()
if err != nil {
return err
}
switch tag {

case 1: // one_of_values.opt_one (oneof,string)
var ov I18NVariable_OptOne
if wt != csproto.WireTypeLengthDelimited {
return fmt.Errorf("incorrect wire type %v for tag field 'opt_one' (tag=1), expected 2 (length-delimited)", wt)
}
if s, err := dec.DecodeString(); err != nil {
return fmt.Errorf("unable to decode string value for field 'opt_one' (tag=1): %w", err)
} else {
ov.OptOne = s
}
m.OneOfValues = &ov
case 2: // one_of_values.opt_two (oneof,string)
var ov I18NVariable_OptTwo
if wt != csproto.WireTypeLengthDelimited {
return fmt.Errorf("incorrect wire type %v for tag field 'opt_two' (tag=2), expected 2 (length-delimited)", wt)
}
if s, err := dec.DecodeString(); err != nil {
return fmt.Errorf("unable to decode string value for field 'opt_two' (tag=2): %w", err)
} else {
ov.OptTwo = s
}
m.OneOfValues = &ov
default:
if skipped, err := dec.Skip(tag, wt); err != nil {
return fmt.Errorf("invalid operation skipping tag %v: %w", tag, err)
} else {
m.XXX_unrecognized = append(m.XXX_unrecognized, skipped...)
}
}
}
return nil
}
123 changes: 123 additions & 0 deletions example/permessage/gogo/gogo_permessage_example_msg.pb.fm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// GENERATED CODE - DO NOT EDIT
// This file was generated by protoc-gen-fastmarshal

package gogo

import (
"fmt"
"sync/atomic"
"github.com/CrowdStrike/csproto"
)

//------------------------------------------------------------------------------
// Custom Protobuf size/marshal/unmarshal code for Msg

// Size calculates and returns the size, in bytes, required to hold the contents of m using the Protobuf
// binary encoding.
func (m *Msg) Size() int {
// nil message is always 0 bytes
if m == nil {
return 0
}
// return cached size, if present
if csz := int(atomic.LoadInt32(&m.XXX_sizecache)); csz > 0 {
return csz
}
// calculate and cache
var sz, l int
_ = l // avoid unused variable

// OneOfValues (oneof)
if m.OneOfValues != nil {
switch typedVal := m.OneOfValues.(type) {
case *Msg_Tags_: // tags (1,message)
l = csproto.Size(typedVal.Tags)
sz += csproto.SizeOfTagKey(1) + csproto.SizeOfVarint(uint64(l)) + l
default:
_ = typedVal // ensure no unused variable
}
}

// cache the size so it can be re-used in Marshal()/MarshalTo()
atomic.StoreInt32(&m.XXX_sizecache, int32(sz))
return sz
}

// Marshal converts the contents of m to the Protobuf binary encoding and returns the result or an error.
func (m *Msg) Marshal() ([]byte, error) {
siz := m.Size()
if siz == 0 {
return []byte{}, nil
}
buf := make([]byte, siz)
err := m.MarshalTo(buf)
return buf, err
}

// MarshalTo converts the contents of m to the Protobuf binary encoding and writes the result to dest.
func (m *Msg) MarshalTo(dest []byte) error {
// nil message == no-op
if m == nil {
return nil
}
var (
enc = csproto.NewEncoder(dest)
buf []byte
err error
extVal interface{}
)
// ensure no unused variables
_ = enc
_ = buf
_ = err
_ = extVal

// OneOfValues (oneof)

if m.OneOfValues != nil {
switch typedVal := m.OneOfValues.(type) {
case *Msg_Tags_: // tags (1,message)
enc.EncodeNested(1, typedVal.Tags)
default:
_ = typedVal // ensure no unused variable
}
}
return nil
}

// Unmarshal decodes a binary encoded Protobuf message from p and populates m with the result.
func (m *Msg) Unmarshal(p []byte) error {
m.Reset()
if len(p) == 0 {
return nil
}
dec := csproto.NewDecoder(p)
for dec.More() {
tag, wt, err := dec.DecodeTag()
if err != nil {
return err
}
switch tag {

case 1: // one_of_values.tags (oneof,message)
var ov Msg_Tags_
if wt != csproto.WireTypeLengthDelimited {
return fmt.Errorf("incorrect wire type %v for tag field 'tags' (tag=1), expected 2 (length-delimited)", wt)
}
var mm_tags Msg_Tags
if err = dec.DecodeNested(&mm_tags); err != nil {
return fmt.Errorf("unable to decode message value for field 'tags' (tag=1): %w", err)
} else {
ov.Tags = &mm_tags
}
m.OneOfValues = &ov
default:
if skipped, err := dec.Skip(tag, wt); err != nil {
return fmt.Errorf("invalid operation skipping tag %v: %w", tag, err)
} else {
m.XXX_unrecognized = append(m.XXX_unrecognized, skipped...)
}
}
}
return nil
}
Loading