Skip to content

Conversation

@youta1119
Copy link
Contributor

Fixes #17

To extend the :map notation, it is now possible to assign template values such as $1, $2, and $3 to the mapping sources.
Template values start with $1, and additional arguments are assigned to $2 and beyond(However, for compatibility reasons, the first argument $1 is optional).
Also, preprocess and postprocess now support additional arguments.

@reedom
Copy link
Owner

reedom commented Mar 12, 2025

Thanks for your PR! I appreciate your contribution to the project. I'll need some time to go through the changes in detail, so I'll get back to you in a few days.

In the meantime, I may provide small suggestions or fixes while reviewing. These will be minor improvements to align with the project's guidelines. Let me know if you have any questions!

return
}
index--
if int(index) >= len(additionalArgs) || index < 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if int(index) >= len(additionalArgs) || index < 0 {
if index < 0 || len(additionalArgs) <= int(index) {

This makes it easier for the human brain to visualize the valid range.
c.f. https://llewellynfalco.blogspot.com/2016/02/dont-use-greater-than-sign-in.html

return nil, logger.Errorf("%v: src type is not defined. make sure to be imported", p.fset.Position(src.Pos()))
}
if util.IsInvalidType(src.Type()) {
if util.IsInvalidType(dst.Type()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

}

func ordinalNumber(n int) string {
if n >= 11 && n <= 13 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if n >= 11 && n <= 13 {
if 11 <= n && n <= 13 {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting to have this function😄
Note: We won't have numbers 100 or more here, so it's okay with the current implementation.

return logger.Errorf(`%v: to use ":reverse", style must be ":style arg"`, p.fset.Position(posReverse))
}

if opts.Reverse && len(opts.TemplatedNameMapper) > 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if opts.Reverse && len(opts.TemplatedNameMapper) > 0 {
if opts.Reverse && 0 < len(opts.TemplatedNameMapper) {

return nil, logger.Errorf("%v: manipulator function %v 2nd arg type mismatch", p.fset.Position(m.Pos), ret.FuncName())
}

if len(m.AdditionalArgs) > 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(m.AdditionalArgs) > 0 {
if 0 < len(m.AdditionalArgs) {

Comment on lines 62 to 65
// reverse cannot be used with additional arguments
if m.Opts.Reverse {
additionalArgs = nil
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youta1119
I was wondering why additionalArgVars is omitted when :reverse is used.
Would it be possible to support both together? Could you share the reasoning behind this decision? Is there a logical constraint that prevents it, or is it just difficult to implement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support additional arguments with :reverse, I believe it is necessary to extend the converter definition syntax to allow defining multiple return values, as shown in the example below.
However, this idea is difficult to implement due to the need for syntax extension.

type Convergen interface {
	// :recv r
	// :reverse
	// :style arg
	ReverseWithAdditionalArg(
		dst *Pet,
	) (src *model.Pet, arg1 int, arg2 string) 
}

As an alternative, I also considered an idea that does not require syntax extension, as in the example below.
However, this idea is complicated because the definition of input arguments is split between the function's arguments and return values.

type Convergen interface {
	// :recv r
	// :reverse
	// :style arg
	ReverseWithAdditionalArg(
		dst *Pet,
		arg1 int, // well be assigned to $2
		arg2 string, // well be assigned to $3
	) (src *model.Pet) // well be assigned to $1
}

I think the former idea is better.
However, it is difficult to implement, so I decided not to implement it in this pull request.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the latter.
But anyway, I'm going to the current PR first, and then I'd like to see if any requests come up regarding this.

Again, thanks for your contribution!

@youta1119 youta1119 force-pushed the feature/support-additional-args branch from fd9fa7b to 8431ba8 Compare March 20, 2025 17:31
@reedom reedom changed the base branch from main to release/0.8.0 March 23, 2025 06:11
@reedom reedom merged commit 10daf29 into reedom:release/0.8.0 Mar 23, 2025
@reedom reedom mentioned this pull request Mar 23, 2025
@apawelec
Copy link

apawelec commented Apr 7, 2025

@youta1119 Great job ❤️

@reedom When can we expect new release with this change?

@reedom
Copy link
Owner

reedom commented Apr 7, 2025

Oh, I've forgotten to put a new tag.

qwenode added a commit to qwenode/convergen that referenced this pull request Apr 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Additonal arguments as mapping source

3 participants