Skip to content

Commit

Permalink
fix!: change return type on Parse method
Browse files Browse the repository at this point in the history
Changes the return type on the `Machine.Parse` method to return a
ConventionalCommit object. This gives the type system more information
on the properties available.

We still get the type information from the `Message` interface because
ConventionalCommit implements Message so no functionality is lose.
However this is does change the return types for all consumers using
the Parse method.

BREAKING CHANGE: `Machine.Parse()` method now returns ConventionalCommit
object instead of Message
  • Loading branch information
lewismiddleton committed Jul 14, 2024
1 parent 2713df9 commit 8aa3fe6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion conventional_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Logger interface {

// Machine represent a FSM able to parse a conventional commit and return it in an structured way.
type Machine interface {
Parse(input []byte) (Message, error)
Parse(input []byte) (*ConventionalCommit, error)
BestEfforter
TypeConfigurer
Logger
Expand Down
2 changes: 1 addition & 1 deletion parser/conventional_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *conventionalCommit) minimal() bool {
return c._type != "" && c.descr != ""
}

func (c *conventionalCommit) export() conventionalcommits.Message {
func (c *conventionalCommit) export() *conventionalcommits.ConventionalCommit {
out := &conventionalcommits.ConventionalCommit{}
out.Exclamation = c.exclamation
out.Type = strings.ToLower(c._type)
Expand Down
2 changes: 1 addition & 1 deletion parser/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func NewMachine(options ...conventionalcommits.MachineOption) conventionalcommit
//
// It can also partially parse input messages returning a partially valid structured representation
// and the error that stopped the parsing.
func (m *machine) Parse(input []byte) (conventionalcommits.Message, error) {
func (m *machine) Parse(input []byte) (*conventionalcommits.ConventionalCommit, error) {
m.data = input
m.p = 0
m.pb = 0
Expand Down
48 changes: 48 additions & 0 deletions parser/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/leodido/go-conventionalcommits"
cctesting "github.com/leodido/go-conventionalcommits/testing"
"github.com/sirupsen/logrus"
logrustest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -79,6 +80,53 @@ func TestMachineBestEffortOption(t *testing.T) {
assert.True(t, p2.HasBestEffort())
}

func TestPublicInterface(t *testing.T) {
i := []byte(`fix(code): correct minor typos in code
see the issue [0] for details
on typos fixed.
[0]: https://issue
Reviewed-by: Z
Refs #133`)
opts := []conventionalcommits.MachineOption{
WithTypes(conventionalcommits.TypesConventional),
}
m, err := NewMachine(opts...).Parse(i)
assert.NoError(t, err)

footers := make(map[string][]string)
footers["reviewed-by"] = []string{"Z"}
footers["refs"] = []string{"133"}

res := &conventionalcommits.ConventionalCommit{
Type: "fix",
Description: "correct minor typos in code",
Scope: cctesting.StringAddress("code"),
Exclamation: false,
Body: cctesting.StringAddress(`see the issue [0] for details
on typos fixed.
[0]: https://issue`),
Footers: footers,
TypeConfig: 1,
}

mScope := *m.Scope
resScope := *res.Scope
mBody := *m.Body
resBody := *res.Body

assert.Equal(t, m.Type, res.Type)
assert.Equal(t, m.Description, res.Description)
assert.Equal(t, mScope, resScope)
assert.Equal(t, m.Exclamation, res.Exclamation)
assert.Equal(t, mBody, resBody)
assert.Equal(t, m.Footers, res.Footers)
assert.Equal(t, m.TypeConfig, res.TypeConfig)
}

func TestMachineTypeConfigOption(t *testing.T) {
p := NewMachine(WithTypes(conventionalcommits.TypesFalco))
mes, err := p.Parse([]byte("new: ciao"))
Expand Down
4 changes: 2 additions & 2 deletions parser/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type testCase struct {
title string
input []byte
ok bool
value conventionalcommits.Message
partialValue conventionalcommits.Message
value *conventionalcommits.ConventionalCommit
partialValue *conventionalcommits.ConventionalCommit
errorString string
bump *conventionalcommits.VersionBump
}
Expand Down

0 comments on commit 8aa3fe6

Please sign in to comment.