-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/frontend: move urlinfo to its own package
These are also being moved so that the code in fetch.go can be moved to a new package without depending on internal/frontend. A couple of functions that were only used by details.go are moved to that file. For #61399 Change-Id: Ic299069ea0b3aaeb80dbbf7f1ed4fedf7d1787df Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/518816 Reviewed-by: Robert Findley <rfindley@google.com> kokoro-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
- Loading branch information
Showing
10 changed files
with
265 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package frontend | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"golang.org/x/pkgsite/internal/experiment" | ||
"golang.org/x/pkgsite/internal/log" | ||
) | ||
|
||
func setExperimentsFromQueryParam(ctx context.Context, r *http.Request) context.Context { | ||
if err := r.ParseForm(); err != nil { | ||
log.Errorf(ctx, "ParseForm: %v", err) | ||
return ctx | ||
} | ||
return newContextFromExps(ctx, r.Form["exp"]) | ||
} | ||
|
||
// newContextFromExps adds and removes experiments from the context's experiment | ||
// set, creates a new set with the changes, and returns a context with the new | ||
// set. Each string in expMods can be either an experiment name, which means | ||
// that the experiment should be added, or "!" followed by an experiment name, | ||
// meaning that it should be removed. | ||
func newContextFromExps(ctx context.Context, expMods []string) context.Context { | ||
var ( | ||
exps []string | ||
remove = map[string]bool{} | ||
) | ||
set := experiment.FromContext(ctx) | ||
for _, exp := range expMods { | ||
if strings.HasPrefix(exp, "!") { | ||
exp = exp[1:] | ||
if set.IsActive(exp) { | ||
remove[exp] = true | ||
} | ||
} else if !set.IsActive(exp) { | ||
exps = append(exps, exp) | ||
} | ||
} | ||
if len(exps) == 0 && len(remove) == 0 { | ||
return ctx | ||
} | ||
for _, a := range set.Active() { | ||
if !remove[a] { | ||
exps = append(exps, a) | ||
} | ||
} | ||
return experiment.NewContext(ctx, exps...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package frontend | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"golang.org/x/pkgsite/internal/experiment" | ||
) | ||
|
||
func TestNewContextFromExps(t *testing.T) { | ||
for _, test := range []struct { | ||
mods []string | ||
want []string | ||
}{ | ||
{ | ||
mods: []string{"c", "a", "b"}, | ||
want: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
mods: []string{"d", "a"}, | ||
want: []string{"a", "b", "c", "d"}, | ||
}, | ||
{ | ||
mods: []string{"d", "!b", "!a", "c"}, | ||
want: []string{"c", "d"}, | ||
}, | ||
} { | ||
ctx := experiment.NewContext(context.Background(), "a", "b", "c") | ||
ctx = newContextFromExps(ctx, test.mods) | ||
got := experiment.FromContext(ctx).Active() | ||
sort.Strings(got) | ||
if !cmp.Equal(got, test.want) { | ||
t.Errorf("mods=%v:\ngot %v\nwant %v", test.mods, got, test.want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.