Skip to content

Pointer issue #15

@chriskolenko

Description

@chriskolenko

u.Input = *new(i)
u.Output = new(o)

After using this new generic method I got stuck with an issue with pointers.

TR;DR;

I think changing the following would make sense;

var in T
u.Input = in
u.Output = new(o)

Long story

I created a generic helper that used an interface as the type. And I wasn't able to use a pointer. Here's the code:

type Command interface {
    AggregateId() uuid.UUID
}

type NewOrg struct {
    aggregateId uuid.UUID
}

func (o *NewOrg) AggregateId() uuid.UUID {
    o.aggregateId
}

func NewCase[T Command]() usecase.Interactor {
	u := usecase.NewInteractor(func(ctx context.Context, in T, out *T) error {
		*out = in
		return nil
	})

	n := new(T)
	u.SetTitle(fmt.Sprintf("Command %T", n))
	u.SetExpectedErrors(es.ErrHandlerNotFound, es.ErrNotCommandHandler)
	u.SetExpectedErrors(status.InvalidArgument)

	return u
}

....

func main() {
	serviceInfo := openapi3.Info{}
	serviceInfo.
		WithTitle(cfg.ServiceName).
		WithVersion(cfg.Version)

	s := web.DefaultService()
	s.OpenAPI.Info = serviceInfo

	s.Use(es.CreateUnit(cli))
	s.Use(middleware.Logger)
	s.Docs("/docs", v4emb.New)
	s.Mount("/debug", middleware.Profiler())
        // Doesn't work.. 
	s.Post("/commands/neworganisation", NewCase[*NewOrg](), nethttp.SuccessStatus(http.StatusCreated))
        // Didn't work with my underlying framework
	s.Post("/commands/neworganisation", NewCase[NewOrg](), nethttp.SuccessStatus(http.StatusCreated))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions