Skip to content

feat: add WithVariableExpansion to expand in default struct tags#46

Merged
alecthomas merged 2 commits into
alecthomas:masterfrom
wsutina:swipawiwat/expand-default
Feb 5, 2026
Merged

feat: add WithVariableExpansion to expand in default struct tags#46
alecthomas merged 2 commits into
alecthomas:masterfrom
wsutina:swipawiwat/expand-default

Conversation

@wsutina

@wsutina wsutina commented Feb 5, 2026

Copy link
Copy Markdown

Currently, default values in struct tags are static strings. This PR adds WithDefaultTransformer marshal option that provides complete flexibility for default value processing.

Comment thread marshal.go Outdated
}
}

func WithVariableExpansion(vars map[string]string) MarshalOption {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Comment! (ironically)

Comment thread marshal.go Outdated
}

if opt != nil && opt.variableExpander != nil {
defaultValue = os.Expand(defaultValue, opt.variableExpander)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't think we want to use os.Expand as it locks us into the syntax supported by it. The option function should just take the whole default value and do whatever it needs to.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Is this what you are thinking?

func WithDefaultTransformer(transformer func(string) string) MarshalOption {
	return func(options *marshalState) {
		options.defaultTransformer = transformer
	}
}

Then at the call site they can use os.Expand if they want?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Implemented it here 7b6ba2b

@wsutina wsutina requested a review from alecthomas February 5, 2026 03:31
@alecthomas

Copy link
Copy Markdown
Owner

Glorious!

@alecthomas alecthomas merged commit 28c7cb6 into alecthomas:master Feb 5, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants