Skip to content

Commit

Permalink
Rename TryXXX() methods to TryGetXXX().
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Apr 22, 2024
1 parent fb0d77c commit 3ca57d0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 65 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ The format is based on [Keep a Changelog], and this project adheres to
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [0.2.1] - 2024-04-23

### Added

- Generate `TryGetXXX()` methods for each option within one-of groups.

## [0.2.0] - 2023-10-19

### Added
Expand Down Expand Up @@ -77,6 +83,7 @@ The format is based on [Keep a Changelog], and this project adheres to
[0.1.4]: https://github.com/dogmatiq/primo/releases/tag/v0.1.4
[0.1.5]: https://github.com/dogmatiq/primo/releases/tag/v0.1.5
[0.2.0]: https://github.com/dogmatiq/primo/releases/tag/v0.2.0
[0.2.1]: https://github.com/dogmatiq/primo/releases/tag/v0.2.1

<!-- version template
## [0.0.1] - YYYY-MM-DD
Expand Down
7 changes: 2 additions & 5 deletions internal/generator/accessor/doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Package accessor generates an accessor method for each type in "one-of
// groups". While "protoc-gen-go" plugin already generates accessor methods for
// for each type in "one-of groups", the generated methods lack the ability to
// differentiate between the absence of a value and the presence of a zero
// value.
// Package accessor generates an accessor method for each field in a message
// that is a member of a "one-of group".
package accessor
16 changes: 10 additions & 6 deletions internal/generator/accessor/oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ import (

func generateForOneOf(code *jen.File, g *scope.OneOfGroup) {
for _, o := range g.Options {
oneOfAccessorTryFunc(code, o)
generateForOneOfOption(code, o)
}
}

func oneOfAccessorTryFunc(code *jen.File, o *scope.OneOfOption) {
methodName := "Try" + o.DiscriminatorFieldName
func generateForOneOfOption(code *jen.File, o *scope.OneOfOption) {
methodName := "TryGet" + o.DiscriminatorFieldName

code.
Commentf(
"%s returns the value of [%s] in one-of field x.%s.",
"%s returns x.%s.%s if x.%s is a [%s].",
methodName,
o.Group.GoFieldName,
o.DiscriminatorFieldName,
o.Group.GoFieldName,
o.DiscriminatorTypeName,
)
code.
Commentf(
"Otherwise, ok is false v is the zero-value.",
)
code.Comment("")
code.Comment("ok returns false if the value of this type is not set.")

code.
Func().
Expand Down
8 changes: 3 additions & 5 deletions internal/test/accessor/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// testAccessor calls a mutator method to set the value and verifies that the
// corresponding accessor returns the expected value and boolean ok value.
// corresponding accessor returns the expected value and an ok value of true.
func testAccessor[M proto.Message, T comparable](
t *testing.T,
mutator func(M, T),
Expand All @@ -27,7 +27,7 @@ func testAccessor[M proto.Message, T comparable](
}

// testAccessorFunc calls a mutator method to set the value and verifies that the
// corresponding accessor returns the expected value and boolean ok value.
// corresponding accessor returns the expected value and an ok value of true.
func testAccessorFunc[M proto.Message, T any](
t *testing.T,
mutate func(M, T),
Expand All @@ -46,9 +46,7 @@ func testAccessorFunc[M proto.Message, T any](

got, gotOK := access(m)
if !gotOK {
t.Fatalf(
"accessor did not return expected ok as true",
)
t.Fatal("accessor did not return with ok set to true")
}

if !eq(got, want) {
Expand Down
99 changes: 50 additions & 49 deletions internal/test/accessor/oneof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,64 @@ import (
func TestOneOfAccessor(t *testing.T) {
t.Parallel()

t.Run("it returns the set value and ok as true to signify the presence of the value", func(t *testing.T) {
testAccessor(
t,
(*OneOf).SetFieldA,
(*OneOf).TryFieldA,
123,
)
t.Run("when the one-of value is set", func(t *testing.T) {
t.Run("it returns the value with ok set to true", func(t *testing.T) {
testAccessor(
t,
(*OneOf).SetFieldA,
(*OneOf).TryGetFieldA,
123,
)

testAccessor(
t,
(*OneOf).SetFieldA,
(*OneOf).TryFieldA,
0,
)
testAccessor(
t,
(*OneOf).SetFieldA,
(*OneOf).TryGetFieldA,
0,
)

testAccessor(
t,
(*OneOf).SetFieldB,
(*OneOf).TryFieldB,
456,
)
testAccessor(
t,
(*OneOf).SetFieldB,
(*OneOf).TryGetFieldB,
456,
)

testAccessor(
t,
(*OneOf).SetFieldB,
(*OneOf).TryFieldB,
0,
)
testAccessor(
t,
(*OneOf).SetFieldB,
(*OneOf).TryGetFieldB,
0,
)

testAccessor(
t,
(*OneOf).SetFieldC,
(*OneOf).TryFieldC,
"<value>",
)
testAccessor(
t,
(*OneOf).SetFieldC,
(*OneOf).TryGetFieldC,
"<value>",
)

testAccessor(
t,
(*OneOf).SetFieldC,
(*OneOf).TryFieldC,
"",
)
testAccessor(
t,
(*OneOf).SetFieldC,
(*OneOf).TryGetFieldC,
"",
)
})
})

t.Run("it ok as false to signify the absence of the value", func(t *testing.T) {
m := &OneOf{}
t.Run("when the one-of value is set to some other option", func(t *testing.T) {
t.Run("it returns with ok set to false", func(t *testing.T) {
m := &OneOf{}
m.SetFieldB(100)

if _, ok := m.TryFieldA(); ok {
t.Fatalf("TryFieldA() returned ok as true, want false")
}
if _, ok := m.TryGetFieldA(); ok {
t.Fatalf("TryGetFieldA() returned ok as true, want false")
}

if _, ok := m.TryFieldB(); ok {
t.Fatalf("TryFieldB() returned ok as true, want false")
}

if _, ok := m.TryFieldC(); ok {
t.Fatalf("TryFieldC() returned ok as true, want false")
}
if _, ok := m.TryGetFieldC(); ok {
t.Fatalf("TryGetFieldC() returned ok as true, want false")
}
})
})
}

0 comments on commit 3ca57d0

Please sign in to comment.