Skip to content

feat: add joinHostPort and splitHostPort network functions#477

Open
ricardbejarano wants to merge 1 commit intoMasterminds:masterfrom
bejaratommy:add-joinHostPort
Open

feat: add joinHostPort and splitHostPort network functions#477
ricardbejarano wants to merge 1 commit intoMasterminds:masterfrom
bejaratommy:add-joinHostPort

Conversation

@ricardbejarano
Copy link
Copy Markdown

Fixes #472 in the upstream project (#472).

What

Adds two new template functions to the network category:

  • joinHostPort host port — wraps net.JoinHostPort. Combines a host and port into host:port, correctly bracketing IPv6 addresses as [host]:port.
  • splitHostPort hostport — wraps net.SplitHostPort. Splits a host:port string into a map[string]string with keys host and port, handling IPv6 bracket notation transparently.

Why

urlJoin/urlParse exist for full URLs, but there was no idiomatic way to construct or parse bare host:port endpoints, which is a common need when templating network configurations (proxies, listeners, upstream addresses). IPv6 addresses require bracket notation that is easy to get wrong manually.

Examples

{{ joinHostPort "192.0.2.1" "80" }}       -> 192.0.2.1:80
{{ joinHostPort "2001:db8::1" "80" }}     -> [2001:db8::1]:80

{{ with splitHostPort "192.0.2.1:80" }}host={{ .host }} port={{ .port }}{{ end }}
-> host=192.0.2.1 port=80

{{ with splitHostPort "[2001:db8::1]:80" }}host={{ .host }} port={{ .port }}{{ end }}
-> host=2001:db8::1 port=80

Changes

  • network.go: implement joinHostPort and splitHostPort
  • network_test.go: unit tests covering IPv4, IPv6, and hostname inputs
  • functions.go: register both functions in the function map and the non-sandbox list
  • docs/network.md: documentation for both functions
  • CHANGELOG.md: entry under unreleased 3.4.0

Fixes Masterminds#472. Adds two new template functions that wrap Go's standard
library net.JoinHostPort and net.SplitHostPort:

- joinHostPort(host, port): combines host and port into host:port,
  correctly bracketing IPv6 addresses as [host]:port.
- splitHostPort(hostport): splits host:port into a map{host, port},
  handling IPv6 bracket notation transparently.
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.

Add joinHostPort

1 participant