Skip to content

Commit

Permalink
pgs (field presence): nit/style fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Gupta <signed@sarthak.sh>
  • Loading branch information
snqk committed Jun 13, 2021
1 parent e0bab48 commit 2d3f0a8
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 36 deletions.
34 changes: 21 additions & 13 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ type Field interface {

// InRealOneOf returns true if the field is in a OneOf of the parent Message.
// This will return false for synthetic oneofs, and will only include 'real' oneofs.
// See: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md
// See: https://github.com/protocolbuffers/protobuf/blob/v3.17.0/docs/field_presence.md
InRealOneOf() bool

// OneOf returns the OneOf that this field is apart of. Nil is returned if
// OneOf returns the OneOf that this field is a part of. Nil is returned if
// the field is not within a OneOf.
OneOf() OneOf

// Type returns the FieldType of this Field.
Type() FieldType

// HasPresence returns true for all fields that have explicit presence as defined by:
// See: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md
// See: https://github.com/protocolbuffers/protobuf/blob/v3.17.0/docs/field_presence.md
HasPresence() bool

// Optional returns whether or not the field is labeled as optional.
Optional() bool
// HasOptionalKeyword returns whether the field is labeled as optional.
HasOptionalKeyword() bool

// Required returns whether or not the field is labeled as required. This
// Required returns whether the field is labeled as required. This
// will only be true if the syntax is proto2.
Required() bool

Expand Down Expand Up @@ -69,25 +69,33 @@ func (f *field) SourceCodeInfo() SourceCodeInfo { return f.info }
func (f *field) Descriptor() *descriptor.FieldDescriptorProto { return f.desc }
func (f *field) Message() Message { return f.msg }
func (f *field) InOneOf() bool { return f.oneof != nil }
func (f *field) InRealOneOf() bool { return f.InOneOf() && !f.desc.GetProto3Optional() }
func (f *field) OneOf() OneOf { return f.oneof }
func (f *field) Type() FieldType { return f.typ }
func (f *field) setMessage(m Message) { f.msg = m }
func (f *field) setOneOf(o OneOf) { f.oneof = o }

func (f *field) InRealOneOf() bool {
return f.InOneOf() && !f.desc.GetProto3Optional()
}

func (f *field) HasPresence() bool {
if f.Type().IsRepeated() || f.Type().IsMap() {
return false
if f.InOneOf() {
return true
}

if f.Type().IsEmbed() {
return true
}

if f.Syntax() == Proto3 && !f.Optional() && !(f.Type().IsEmbed() || f.InRealOneOf()) {
return false
if f.HasOptionalKeyword() ||
(f.Syntax() == Proto2 && !f.Type().IsRepeated() && !f.Type().IsMap()) {
return true
}

return true
return false
}

func (f *field) Optional() bool {
func (f *field) HasOptionalKeyword() bool {
if f.Syntax() == Proto3 {
return f.desc.GetProto3Optional()
}
Expand Down
12 changes: 6 additions & 6 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestField_InRealOneOf(t *testing.T) {
func TestField_HasPresence(t *testing.T) {
t.Parallel()

f := &field{}
f := dummyField()
f.addType(&repT{scalarT: &scalarT{}})
assert.False(t, f.HasPresence())

Expand All @@ -137,19 +137,19 @@ func TestField_Optional(t *testing.T) {
optLabel := descriptor.FieldDescriptorProto_LABEL_OPTIONAL

f := &field{msg: &msg{parent: dummyFile()}}
assert.False(t, f.Optional())
assert.False(t, f.HasOptionalKeyword())

f.desc = &descriptor.FieldDescriptorProto{Label: &optLabel}
assert.False(t, f.Optional())
assert.False(t, f.HasOptionalKeyword())

f = dummyField()
assert.False(t, f.Optional())
assert.False(t, f.HasOptionalKeyword())

f = dummyOneOfField(false)
assert.False(t, f.Optional())
assert.False(t, f.HasOptionalKeyword())

f = dummyOneOfField(true)
assert.True(t, f.Optional())
assert.True(t, f.HasOptionalKeyword())
}

func TestField_Type(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *scalarT) Element() FieldTypeElem { return nil }
func (s *scalarT) Key() FieldTypeElem { return nil }

func (s *scalarT) IsOptional() bool {
return s.fld.Optional()
return s.fld.HasOptionalKeyword()
}

func (s *scalarT) IsRequired() bool {
Expand Down
10 changes: 4 additions & 6 deletions init_option.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pgs

import (
"google.golang.org/protobuf/types/pluginpb"
"io"
"os"

Expand Down Expand Up @@ -49,11 +48,10 @@ func BiDirectional() InitOption {
return func(g *Generator) { g.workflow = &onceWorkflow{workflow: &standardWorkflow{BiDi: true}} }
}

// SupportProto3Optional adds support for proto3 field presence.
// See: https://github.com/protocolbuffers/protobuf/blob/master/docs/implementing_proto3_presence.md#signaling-that-your-code-generator-supports-proto3-optional
func SupportProto3Optional() InitOption {
// SupportedFeatures allows defining protoc features to enable / disable.
// See: https://github.com/protocolbuffers/protobuf/blob/v3.17.0/docs/implementing_proto3_presence.md#signaling-that-your-code-generator-supports-proto3-optional
func SupportedFeatures(feat *uint64) InitOption {
return func(g *Generator) {
p3Optional := uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
g.persister.SetSupportedField(&p3Optional)
g.persister.SetSupportedFeatures(feat)
}
}
4 changes: 2 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ type Message interface {
OneOfFields() []Field

// SyntheticOneOfFields returns only the fields contained within synthetic OneOf blocks.
// See: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md
// See: https://github.com/protocolbuffers/protobuf/blob/v3.17.0/docs/field_presence.md
SyntheticOneOfFields() []Field

// OneOfs returns the OneOfs contained within this Message.
OneOfs() []OneOf

// RealOneOfs returns the OneOfs contained within this Message.
// This excludes synthetic OneOfs.
// See: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md
// See: https://github.com/protocolbuffers/protobuf/blob/v3.17.0/docs/field_presence.md
RealOneOfs() []OneOf

// Extensions returns all of the Extensions applied to this Message.
Expand Down
2 changes: 1 addition & 1 deletion oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type OneOf interface {
Fields() []Field

// IsSynthetic returns true if this is a proto3 synthetic oneof.
// See: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md
// See: https://github.com/protocolbuffers/protobuf/blob/v3.17.0/docs/field_presence.md
IsSynthetic() bool

setMessage(m Message)
Expand Down
12 changes: 6 additions & 6 deletions persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ import (
type persister interface {
SetDebugger(d Debugger)
SetFS(fs afero.Fs)
SetSupportedField(f *uint64)
SetSupportedFeatures(f *uint64)
AddPostProcessor(proc ...PostProcessor)
Persist(a ...Artifact) *plugin_go.CodeGeneratorResponse
}

type stdPersister struct {
Debugger

fs afero.Fs
procs []PostProcessor
supportedField *uint64
fs afero.Fs
procs []PostProcessor
supportedFeatures *uint64
}

func newPersister() *stdPersister { return &stdPersister{fs: afero.NewOsFs()} }

func (p *stdPersister) SetDebugger(d Debugger) { p.Debugger = d }
func (p *stdPersister) SetFS(fs afero.Fs) { p.fs = fs }
func (p *stdPersister) SetSupportedField(f *uint64) { p.supportedField = f }
func (p *stdPersister) SetSupportedFeatures(f *uint64) { p.supportedFeatures = f }
func (p *stdPersister) AddPostProcessor(proc ...PostProcessor) { p.procs = append(p.procs, proc...) }

func (p *stdPersister) Persist(arts ...Artifact) *plugin_go.CodeGeneratorResponse {
resp := new(plugin_go.CodeGeneratorResponse)
resp.SupportedFeatures = p.supportedField
resp.SupportedFeatures = p.supportedFeatures

for _, a := range arts {
switch a := a.(type) {
Expand Down
2 changes: 1 addition & 1 deletion proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
// Proto3 syntax permits the use of "optional" field presence. Non optional fields default to the zero
// value of that particular type if not defined.
// Most of the field types in the generated go structs are value types.
// See: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md#presence-in-proto3-apis
// See: https://github.com/protocolbuffers/protobuf/blob/v3.17.0/docs/field_presence.md#presence-in-proto3-apis
Proto3 Syntax = "proto3"
)

Expand Down

0 comments on commit 2d3f0a8

Please sign in to comment.