Skip to content

Return ConventionalCommit content in public interface #36

Open
@lewismiddleton

Description

@lewismiddleton

When using this package, I found that the return type of the Parse method didn't give me access to any of the attributes on the ConventionalCommit object.

Minimal Reproducer
package main

import (
	"fmt"

	"github.com/leodido/go-conventionalcommits"
	"github.com/leodido/go-conventionalcommits/parser"
)

// Example from https://github.com/leodido/go-conventionalcommits/blob/2713df998a1596207af6090215be9cbe2fd651be/parser/example_test.go#L81
func main() {
	i := []byte(`fix: 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{
		parser.WithTypes(conventionalcommits.TypesConventional),
	}
	m, _ := parser.NewMachine(opts...).Parse(i)

	// fmt.Printf("%+v", m)
	fmt.Println(m.Description)
}

When running with the code above, I get:

./main.go:27:16: m.Description undefined (type conventionalcommits.Message has no field or method Description)

If you comment out the line referencing the Description and dump the object you get:

&{Type:fix Description:correct minor typos in code Scope:<nil> Exclamation:false Body:0x140001181b8 Footers:map[refs:[133] reviewed-by:[Z]] TypeConfig:1}⏎                                                                                        

I think this is because the Parse method returns a Message object instead of a ConventionalCommit

Even though the attributes get specified in the object, the type system isn't aware that those properties are available.

I think the fix for this is changing the return types to be the more specific *conventionalcommits.ConventionalCommit. This shouldn't lose any functionality because the ConventionalCommit struct implements the Message interface (it took me a quick reference to how interfaces work to figure this out)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions