Makpoc/gomigen

Add ability to access method parameters

Closed this issue · 2 comments

I'm trying to use gomigen for a project at work. I have an audit logging layer on my services that records every operation in a database. Since the code is mostly the same for all methods I'd like to use a code generation tool. However, I'm not able to use gomigen, since there is no option to access the parameters of the method. I need to access them in order to preserve them in the database as part of the audit log.

Here's how the current implementation looks (I've stripped some details):

func (s *Service) Operation(ctx context.Context, p *Params) (*Result, error) {
	user, err := auth.UserFromContext(ctx)
	if err != nil {
		return nil, fmt.Errorf("no user info provided: %w", err)
	}

	ri, err := remoteinfo.FromContext(ctx)
	if err != nil {
		return nil, fmt.Errorf("error extracting remote info: %w", err)
	}

	log, err := s.logger.Start(ctx, &audit.Log{
		UserID:    user.ID,
		ClientID:  user.ClientID,
		Action:    "package.Service.Operation",
		Params:    p, // Params need to be persisted.
		UserAgent: ri.UserAgent,
		RemoteIP:  ri.RemoteIP,
		Country:   ri.Country,
	})

	if err != nil {
		return nil, fmt.Errorf("error persisting start audit log: %w", err)
	}

	ctx = audit.LogInContext(ctx, log)
	r, err := s.next.Operation(ctx, p)

	_ = s.logger.End(ctx, log)

	return r, err
}

The types.MethodInfo object seems like a good place to add the method parameters. Perhaps a Params []interface{} field will be enough. Does this sound like a good idea and does it fit the general design idea of gomigen?

Thanks,
Ivan

Hi. This was implemented as part of #7. Do note that it's a breaking change because it extends the MethodInfo structure. Let me know if you need a tag as of now or you can wait until I look into #5

Thanks!

I don't need a tag, I'll try this out and I'll add panics in order to stop the original method invocation (so I can actually try it) until #5 is ready.