amenzhinsky / dbus-codegen-go

D-Bus code-generation for golang
MIT License
18 stars 14 forks source link

Unused imports in generated code with `-only` #11

Open mdegat01 opened 2 years ago

mdegat01 commented 2 years ago

When using the -only option to limit the generated file to just the needed interface the generator still adds imports of "fmt" and "errors" at the top. Those imports are only used in the Signal section of the generated code shown below and that section isn't included in the generated file when -only is used. These imports have to be manually removed after generation right now in order for the file to compile.

Signal section

// Signal is a common interface for all signals.
type Signal interface {
    Name() string
    Interface() string
    Sender() string

    path() dbus.ObjectPath
    values() []interface{}
}

// ErrUnknownSignal is returned by LookupSignal when a signal cannot be resolved.
var ErrUnknownSignal = errors.New("unknown signal")

// LookupSignal converts the given raw D-Bus signal with variable body
// into one with typed structured body or returns ErrUnknownSignal error.
func LookupSignal(signal *dbus.Signal) (Signal, error) {
    switch signal.Name {
    case InterfaceOrgFreedesktopDBusProperties + "." + "PropertiesChanged":
        v0, ok := signal.Body[0].(string)
        if !ok {
            return nil, fmt.Errorf("prop .Interface is %T, not string", signal.Body[0])
        }
        v1, ok := signal.Body[1].(map[string]dbus.Variant)
        if !ok {
            return nil, fmt.Errorf("prop .ChangedProperties is %T, not map[string]dbus.Variant", signal.Body[1])
        }
        v2, ok := signal.Body[2].([]string)
        if !ok {
            return nil, fmt.Errorf("prop .InvalidatedProperties is %T, not []string", signal.Body[2])
        }
        return &OrgFreedesktopDBusPropertiesPropertiesChangedSignal{
            sender: signal.Sender,
            Path:   signal.Path,
            Body: &OrgFreedesktopDBusPropertiesPropertiesChangedSignalBody{
                Interface:             v0,
                ChangedProperties:     v1,
                InvalidatedProperties: v2,
            },
        }, nil
    default:
        return nil, ErrUnknownSignal
    }
}