[go] net/url: add JoinPath, URL.JoinPath

321 views
Skip to first unread message

Gerrit Dou (Gerrit)

unread,
Dec 28, 2021, 9:51:38 PM12/28/21
to goph...@pubsubhelper.golang.org, Carl Johnson, golang-co...@googlegroups.com

Gerrit Dou has uploaded this change for review.

View Change

net/url: add JoinPath, URL.JoinPath

Builds on CL 332209.

Fixes #47005

Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 41a5d34b1b74e8ee8bef1a92e47d52aecf9c7e53
GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
M api/except.txt
3 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/api/except.txt b/api/except.txt
index b9972c1..37c76a3 100644
--- a/api/except.txt
+++ b/api/except.txt
@@ -505,3 +505,5 @@
pkg unicode, const Version = "7.0.0"
pkg unicode, const Version = "8.0.0"
pkg unicode, const Version = "9.0.0"
+pkg net/url, func JoinPath(string, ...string) (string, error)
+pkg net/url, method (*URL) JoinPath(...string) *URL
\ No newline at end of file
diff --git a/src/net/url/url.go b/src/net/url/url.go
index f31aa08..286e6aa 100644
--- a/src/net/url/url.go
+++ b/src/net/url/url.go
@@ -13,6 +13,7 @@
import (
"errors"
"fmt"
+ "path"
"sort"
"strconv"
"strings"
@@ -1176,6 +1177,17 @@
return nil
}

+// JoinPath returns a new URL with the provided path elements
+// joined to any existing path and cleaned of any ./ or ../ elements.
+func (u *URL) JoinPath(elem ...string) *URL {
+ url := *u
+ if len(elem) > 0 {
+ elem = append([]string{u.Path}, elem...)
+ url.setPath(path.Join(elem...))
+ }
+ return &url
+}
+
// validUserinfo reports whether s is a valid userinfo string per RFC 3986
// Section 3.2.1:
// userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
@@ -1216,3 +1228,13 @@
}
return false
}
+
+// JoinPath concatenates the path elements to the base URL and cleans any ./ or ../ elements from the final URL string.
+func JoinPath(base string, elem ...string) (result string, err error) {
+ url, err := Parse(base)
+ if err != nil {
+ return
+ }
+ result = url.JoinPath(elem...).String()
+ return
+}
diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go
index 664757b..268ba56 100644
--- a/src/net/url/url_test.go
+++ b/src/net/url/url_test.go
@@ -2062,3 +2062,74 @@
})
}
}
+
+func TestJoinPath(t *testing.T) {
+ type args struct {
+ baseUrl string
+ elem []string
+ }
+ tests := []struct {
+ name string
+ args args
+ wantResult string
+ wantErr bool
+ }{
+ {
+ name: "test normal url",
+ args: args{
+ baseUrl: "https://go.googlesource.com",
+ elem: []string{"go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test .. parent url",
+ args: args{
+ baseUrl: "https://go.googlesource.com/a/b/c",
+ elem: []string{"../../../go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test . cul path",
+ args: args{
+ baseUrl: "https://go.googlesource.com/",
+ elem: []string{"./go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test multiple Separator",
+ args: args{
+ baseUrl: "https://go.googlesource.com//",
+ elem: []string{"/go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test more elems",
+ args: args{
+ baseUrl: "https://go.googlesource.com//",
+ elem: []string{"/go", "a", "b", "c"},
+ },
+ wantResult: "https://go.googlesource.com/go/a/b/c",
+ wantErr: false,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotResult, err := JoinPath(tt.args.baseUrl, tt.args.elem...)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("JoinPath() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ if gotResult != tt.wantResult {
+ t.Errorf("JoinPath() = %v, want %v", gotResult, tt.wantResult)
+ }
+ })
+ }
+}

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Dou <letsus...@gmail.com>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-MessageType: newchange

Gerrit Dou (Gerrit)

unread,
Dec 28, 2021, 9:54:57 PM12/28/21
to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Dou uploaded patch set #2 to this change.

View Change

net/url: add JoinPath, URL.JoinPath

Builds on CL 332209.

Fixes #47005

Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 5a67f0efd32770d6ba8d98aa79dd1a8210fc6994

GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 108 insertions(+), 0 deletions(-)

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Dou <letsus...@gmail.com>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-MessageType: newpatchset

Carl Johnson (Gerrit)

unread,
Feb 22, 2022, 11:53:44 AM2/22/22
to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, Russ Cox, Damien Neil, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil.

View Change

1 comment:

  • Patchset:

    • Patch Set #2:

      The try bot error is

      Error running API checker: exit status 1


    • +pkg net/url, func JoinPath(string, ...string) (string, error)
      +pkg net/url, method (*URL) JoinPath(...string) *URL

    • exit status 1
      2022/02/22 16:37:33 Failed: exit status 1

      Do the public API definitions need to be updated when adding a new method to url.URL? How does that work?

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Comment-Date: Tue, 22 Feb 2022 16:53:41 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Mar 3, 2022, 2:52:47 PM3/3/22
to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Carl Johnson, Damien Neil.

Gerrit Bot uploaded patch set #4 to this change.

View Change

net/url: add JoinPath, URL.JoinPath

Builds on CL 332209.

Fixes #47005

Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 565ccd029cbb9806a9589b600a8b6e276b750d55

GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 99 insertions(+), 0 deletions(-)

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 4
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-MessageType: newpatchset

Gerrit Bot (Gerrit)

unread,
Mar 3, 2022, 3:04:31 PM3/3/22
to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Carl Johnson, Damien Neil.

Gerrit Bot uploaded patch set #5 to this change.

View Change

net/url: add JoinPath, URL.JoinPath

Builds on CL 332209.

Fixes #47005

Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 1910d6613e6763de1961017740d107cfc09f205d

GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 106 insertions(+), 0 deletions(-)

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 5

Carl Johnson (Gerrit)

unread,
Mar 3, 2022, 3:08:33 PM3/3/22
to Gerrit Bot, Russ Cox, goph...@pubsubhelper.golang.org, Gopher Robot, Damien Neil, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil, Russ Cox.

View Change

3 comments:

  • File src/net/url/url.go:

    • Patch Set #3, Line 1232: // JoinPath concatenates the path elements to the base URL and cleans any ./ or ../ elements from the final URL string.

      The wording here seems unnecessarily different from the wording in URL.JoinPath. […]

      Done

  • File src/net/url/url_test.go:

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 3
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Comment-Date: Thu, 03 Mar 2022 20:08:29 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Russ Cox <r...@golang.org>
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Mar 3, 2022, 3:52:34 PM3/3/22
to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil.

Gerrit Bot uploaded patch set #6 to this change.

View Change

net/url: add JoinPath, URL.JoinPath

Builds on CL 332209.

Fixes #47005

Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: c06c3ce8379546f6135a6385a24c19422a382a19

GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 94 insertions(+), 0 deletions(-)

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-MessageType: newpatchset

Carl Johnson (Gerrit)

unread,
Mar 3, 2022, 3:53:21 PM3/3/22
to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, Russ Cox, Damien Neil, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil, Russ Cox.

View Change

6 comments:

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 5
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Comment-Date: Thu, 03 Mar 2022 20:53:17 +0000

Carl Johnson (Gerrit)

unread,
Mar 3, 2022, 10:23:51 PM3/3/22
to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, Russ Cox, Damien Neil, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil.

View Change

1 comment:


    • Error running API checker: exit status 1
      +pkg net/url, func JoinPath(string, ...string) (string, error)
      +pkg net/url, method (*URL) JoinPath(...string) *URL
      exit status 1

    • 2022/03/04 03:17:17 Failed: exit status 1
      go tool dist: FAILED

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 6
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Comment-Date: Fri, 04 Mar 2022 03:23:47 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Mar 4, 2022, 9:49:58 AM3/4/22
to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil.

Gerrit Bot uploaded patch set #7 to this change.

View Change

net/url: add JoinPath, URL.JoinPath

Builds on CL 332209.

Fixes #47005

Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 51b735066eef74f5e67c3e8899c58f44c0383c61
GitHub-Pull-Request: golang/go#50383
---
M api/next.txt
M src/net/url/url.go
M src/net/url/url_test.go
3 files changed, 96 insertions(+), 0 deletions(-)

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 7
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-MessageType: newpatchset

Ian Lance Taylor (Gerrit)

unread,
Mar 9, 2022, 1:05:02 AM3/9/22
to Gerrit Bot, Carl Johnson, Russ Cox, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Gopher Robot, Damien Neil, golang-co...@googlegroups.com

Attention is currently required from: Carl Johnson, Damien Neil, Russ Cox.

View Change

1 comment:

To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
Gerrit-Change-Number: 374654
Gerrit-PatchSet: 8
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Comment-Date: Wed, 09 Mar 2022 06:04:58 +0000

Ian Lance Taylor (Gerrit)

unread,
Mar 9, 2022, 1:05:45 AM3/9/22
to Gerrit Bot, Carl Johnson, Russ Cox, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Gopher Robot, Damien Neil, golang-co...@googlegroups.com

Attention is currently required from: Carl Johnson, Damien Neil, Russ Cox.

Patch set 8:Trust +1

View Change

    To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
    Gerrit-Change-Number: 374654
    Gerrit-PatchSet: 8
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Russ Cox <r...@golang.org>
    Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
    Gerrit-Attention: Carl Johnson <m...@carlmjohnson.net>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Attention: Russ Cox <r...@golang.org>
    Gerrit-Comment-Date: Wed, 09 Mar 2022 06:05:41 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Damien Neil (Gerrit)

    unread,
    Mar 9, 2022, 1:03:16 PM3/9/22
    to Gerrit Bot, Carl Johnson, Russ Cox, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Carl Johnson, Russ Cox.

    Patch set 8:Code-Review +2

    View Change

      To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
      Gerrit-Change-Number: 374654
      Gerrit-PatchSet: 8
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
      Gerrit-Attention: Carl Johnson <m...@carlmjohnson.net>
      Gerrit-Attention: Russ Cox <r...@golang.org>
      Gerrit-Comment-Date: Wed, 09 Mar 2022 18:03:13 +0000

      Ian Lance Taylor (Gerrit)

      unread,
      Mar 9, 2022, 8:20:02 PM3/9/22
      to Gerrit Bot, Carl Johnson, Russ Cox, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Damien Neil, Gopher Robot, golang-co...@googlegroups.com

      Attention is currently required from: Carl Johnson, Russ Cox.

      Patch set 8:Run-TryBot +1

      View Change

        To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
        Gerrit-Change-Number: 374654
        Gerrit-PatchSet: 8
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
        Gerrit-Attention: Carl Johnson <m...@carlmjohnson.net>
        Gerrit-Attention: Russ Cox <r...@golang.org>
        Gerrit-Comment-Date: Thu, 10 Mar 2022 01:19:58 +0000

        Gopher Robot (Gerrit)

        unread,
        Mar 9, 2022, 8:34:21 PM3/9/22
        to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Ian Lance Taylor, Damien Neil, Russ Cox, golang-co...@googlegroups.com

        Gopher Robot submitted this change.

        View Change


        Approvals: Russ Cox: Looks good to me, approved; Automatically submit change Damien Neil: Looks good to me, approved Ian Lance Taylor: Trusted; Run TryBots Gopher Robot: TryBots succeeded
        net/url: add JoinPath, URL.JoinPath

        Builds on CL 332209.

        Fixes #47005

        Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
        GitHub-Last-Rev: 51b735066eef74f5e67c3e8899c58f44c0383c61
        GitHub-Pull-Request: golang/go#50383
        Reviewed-on: https://go-review.googlesource.com/c/go/+/374654
        Reviewed-by: Russ Cox <r...@golang.org>
        Auto-Submit: Russ Cox <r...@golang.org>
        Trust: Ian Lance Taylor <ia...@golang.org>
        Reviewed-by: Damien Neil <dn...@google.com>
        Run-TryBot: Ian Lance Taylor <ia...@golang.org>
        TryBot-Result: Gopher Robot <go...@golang.org>

        ---
        M api/next.txt
        M src/net/url/url.go
        M src/net/url/url_test.go
        3 files changed, 103 insertions(+), 0 deletions(-)

        diff --git a/api/next.txt b/api/next.txt
        index 23fd98a..148cbff 100644
        --- a/api/next.txt
        +++ b/api/next.txt
        @@ -3,3 +3,5 @@
        pkg encoding/binary, type AppendByteOrder interface, AppendUint32([]uint8, uint32) []uint8
        pkg encoding/binary, type AppendByteOrder interface, AppendUint64([]uint8, uint64) []uint8
        pkg encoding/binary, type AppendByteOrder interface, String() string

        +pkg net/url, func JoinPath(string, ...string) (string, error)
        +pkg net/url, method (*URL) JoinPath(...string) *URL
        diff --git a/src/net/url/url.go b/src/net/url/url.go
        index f31aa08..1571bf7 100644

        --- a/src/net/url/url.go
        +++ b/src/net/url/url.go
        @@ -13,6 +13,7 @@
        import (
        "errors"
        "fmt"
        + "path"
        "sort"
        "strconv"
        "strings"
        @@ -1176,6 +1177,17 @@
        return nil
        }

        +// JoinPath returns a new URL with the provided path elements joined to
        +// any existing path and the resulting path cleaned of any ./ or ../ elements.

        +func (u *URL) JoinPath(elem ...string) *URL {
        + url := *u
        + if len(elem) > 0 {
        + elem = append([]string{u.Path}, elem...)
        + url.setPath(path.Join(elem...))
        + }
        + return &url
        +}
        +
        // validUserinfo reports whether s is a valid userinfo string per RFC 3986
        // Section 3.2.1:
        // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
        @@ -1216,3 +1228,14 @@
        }
        return false
        }
        +
        +// JoinPath returns a URL string with the provided path elements joined to
        +// the existing path of base and the resulting path cleaned of any ./ or ../ elements.

        +func JoinPath(base string, elem ...string) (result string, err error) {
        + url, err := Parse(base)
        + if err != nil {
        + return
        + }
        + result = url.JoinPath(elem...).String()
        + return
        +}
        diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go
        index 664757b..84dba45 100644
        --- a/src/net/url/url_test.go
        +++ b/src/net/url/url_test.go
        @@ -2062,3 +2062,59 @@

        })
        }
        }
        +
        +func TestJoinPath(t *testing.T) {
        +	tests := []struct {
        + base string
        + elem []string
        + out string
        + }{
        + {
        + base: "https://go.googlesource.com",

        + elem: []string{"go"},
        +			out:  "https://go.googlesource.com/go",
        + },
        + {
        + base: "https://go.googlesource.com/a/b/c",

        + elem: []string{"../../../go"},
        +			out:  "https://go.googlesource.com/go",
        + },
        + {
        + base: "https://go.googlesource.com/",

        + elem: []string{"./go"},
        +			out:  "https://go.googlesource.com/go",
        + },
        + {
        + base: "https://go.googlesource.com//",

        + elem: []string{"/go"},
        +			out:  "https://go.googlesource.com/go",
        + },
        + {
        + base: "https://go.googlesource.com//",

        + elem: []string{"/go", "a", "b", "c"},
        +			out:  "https://go.googlesource.com/go/a/b/c",
        + },
        + {
        + base: "http://[fe80::1%en0]:8080/",

        + elem: []string{"/go"},
        + },
        + }
        +	for _, tt := range tests {
        +		wantErr := "nil"
        + if tt.out == "" {
        + wantErr = "non-nil error"
        + }
        + if out, err := JoinPath(tt.base, tt.elem...); out != tt.out || (err == nil) != (tt.out != "") {
        + t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
        + }
        + var out string
        + u, err := Parse(tt.base)
        + if err == nil {
        + u = u.JoinPath(tt.elem...)
        + out = u.String()
        + }
        + if out != tt.out || (err == nil) != (tt.out != "") {
        + t.Errorf("Parse(%q).JoinPath(%q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
        + }
        + }
        +}

        To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
        Gerrit-Change-Number: 374654
        Gerrit-PatchSet: 9
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
        Gerrit-MessageType: merged
        Reply all
        Reply to author
        Forward
        0 new messages