This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
/
url.go
52 lines (47 loc) · 1.64 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2022 Daniel Erat.
// All rights reserved.
package seed
import (
"net/http"
"net/url"
"strconv"
)
// URL holds data used to seed forms with an entity's relationship to a URL.
type URL struct {
// URL contains the full URL.
URL string
// LinkType contains the link type ID.
// Applicable link types should end in "<Entity>_URL_Link", depending on the
// type of the entity being linked to the URL (but note that the LinkType
// enum may not include all possible values).
LinkType LinkType
}
// setParams sets query parameters in vals corresponding to non-empty fields in url.
// The supplied prefix (e.g. "urls.0.") is prepended before each parameter name.
// method contains the HTTP method that will be used (e.g. "GET" or "POST").
func (url *URL) setParams(vals url.Values, prefix, method string) {
// Weirdly, recordings (or maybe all forms seeded with GET) use different
// field names for URLs from the ones that are documented at
// https://wiki.musicbrainz.org/Development/Release_Editor_Seeding
// (which uses POST). The place where I finally found the alternate names is
// https://github.com/metabrainz/musicbrainz-server/blob/master/root/static/scripts/edit/externalLinks.js:
//
// const seededLinkRegex = new RegExp(
// '(?:\\?|&)edit-' + sourceType +
// '\\.url\\.([0-9]+)\\.(text|link_type_id)=([^&]+)',
// 'g',
// );
ifGet := func(get, post string) string {
if method == http.MethodGet {
return get
}
return post
}
if url.URL != "" {
vals.Set(prefix+ifGet("text", "url"), url.URL)
}
if url.LinkType != 0 {
vals.Set(prefix+ifGet("link_type_id", "link_type"),
strconv.Itoa(int(url.LinkType)))
}
}