Skip to content

🐛 Bug: maker.Make() expects a struct's definition in all input files #69

@grivera64

Description

@grivera64

Description

Currently, /maker/maker.go's Make() method expects a struct definition to exist in all files, rather than just one. It would not be possible to include an already defined struct several times within one package, so any case with multiple files would fail.

How to Reproduce

Example Code

// File: file1.go
package mypackage

type MyStruct struct {
}
// File: file2.go
package mypackage

import (
    "fmt"
)

func (ms *MyStruct) SayHelloWorld() {
    fmt.Println("Hello World!")
}

Example CLI arguments

ifacemaker --file file1.go --file file2.go --struct MyStruct --iface MyInterface --pkg mypackage --output my_interface.go --not-exported true --iface-comment "MyInterface is implemented by MyStruct."

Expected Output:

// File: my_interface.go
package mypackage

type MyInterface interface {
    SayHelloWorld()
}

Error

XXXX/XX/XX XX:XX:XX "MyStruct" structtype not found in input files

Proposed Solution

We could update the code to only require one definition of the struct to be found within all files. After that, we could update the second pass in Make() to only parse the struct in the found file (we would need to store which file is was found on in the first pass for this) and parse the methods separately.

Drawbacks

This may require refactoring the ParseStruct() method to ensure no bugs occur in the future, since it currently parses both the struct and associated methods in a single step. As a result, the bug fix could also include splitting ParseStruct() into sub-methods if we run into any issues.

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