This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.go
67 lines (57 loc) · 1.69 KB
/
seed.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2022 Daniel Erat.
// All rights reserved.
// Package seed generates URLs that pre-fill fields when adding entities to MusicBrainz.
package seed
import (
"context"
"net/url"
"github.com/derat/yambs/db"
)
//go:generate go run gen/gen_enums.go
const (
maxDescLen = 40 // max length for description components
mbidPrefixLen = 8
)
// Type describes a type of MusicBrainz entity being edited.
type Type string
const (
RecordingType Type = "recording"
ReleaseType Type = "release"
InfoType Type = "info"
)
// Edit represents a seeded MusicBrainz edit.
type Edit interface {
// Type returns the type of entity being edited.
Type() Type
// Description returns a human-readable description of the edit.
Description() string
// URL returns a URL to seed the edit form.
URL() string
// Params returns form values that should be sent to seed the edit form.
// Note that some parameters contain multiple values (i.e. don't call Get()).
Params() url.Values
// Method() returns the HTTP method that should be used for the request for URL.
// GET is preferable since it avoids an anti-CSRF interstitial page.
Method() string
// Finish fixes up fields in the edit.
// This should be called once after filling the edit's fields.
// This only exists because recordings are dumb and require
// artists' database IDs rather than their MBIDs.
Finish(ctx context.Context, db *db.DB) error
}
func truncate(orig string, max int, ellide bool) string {
if len(orig) <= max {
return orig
}
if ellide {
return orig[:max-1] + "…"
}
return orig[:max]
}
func setParams(vals url.Values, m map[string]string, prefix string) {
for k, v := range m {
if v != "" {
vals.Set(prefix+k, v)
}
}
}