masaushi / accessory

accessor methods generator for Go programming language
MIT License
17 stars 6 forks source link

Identical package names breaks accessory generation #70

Closed timrabl closed 5 months ago

timrabl commented 9 months ago

First of all, thank you for your brilliant generator!

I'm currently expecting issues while generating setter and getter methods for a struct that relies on two packages that are named identical. While renaming imports is supported in GoLang (see), i'm not sure if this is possible to implement.

So, for example:

//types.go

package main

import (
  v1 "example.org/a/v1" // exported as a, but renamed import to v1
  v2 "example.org/a/v2" // exported as a, but renamed import to v2
)

//go:generate accessory -type MyStruct -receiver myStruct
type MyStruct struct {
  dummy1 *v1.Dummy `accessor:"getter:GetDummy1,setter:SetDummy1"`
  dummy2 *v2.Dummy `accessor:"getter:GetDummy2,setter:SetDummy2"`
}
// my_struct_accessor.go
// Code generated by accessory; DO NOT EDIT.

import (
  "example.org/a/v1"
  "example.org/a/v2"
)

func (myStruct *MyStruct) GetDummy1() *a.Dummy {
    if myStruct == nil {
        return nil
    }
    return myStruct.dummy1
}

func (myStruct *MyStruct) SetDummy1(val *a.Dummy) {
    if myStruct == nil {
        return
    }
    myStruct.dummy1 = val
}

func (myStruct *MyStruct) GetDummy2() *a.Dummy {
    if myStruct == nil {
        return nil
    }
    return myStruct.dummy2
}

func (myStruct *MyStruct) SetDummy2(val *a.Dummy) {
    if myStruct == nil {
        return
    }
    myStruct.dummy2 = val
}
masaushi commented 8 months ago

Thank you for reporting an issue! It will take time to fix the problem but I will investigate it when I have time.

masaushi commented 5 months ago

I fixed the bug and released the new 3.0.1 version, so please try it out!