/
manifest.go
84 lines (72 loc) · 3.15 KB
/
manifest.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package v1
import (
"github.com/cosmos/atlas/server/models"
)
type (
// ModuleManifest defines the primary module fields in a module's manifest.
ModuleManifest struct {
Name string `json:"name" toml:"name" validate:"required"`
Keywords []string `json:"keywords" toml:"keywords" validate:"omitempty,gt=0,unique,dive,gt=0"`
Description string `json:"description" toml:"description"`
Homepage string `json:"homepage" toml:"homepage" validate:"omitempty,url"`
}
// AuthorsManifest defines author information in a module's manifest.
AuthorsManifest struct {
Name string `json:"name" toml:"name" validate:"required"`
Email string `json:"email" toml:"email" validate:"omitempty,email"`
}
// BugTackerManifest defines the bug tracker information in a module's manifest.
BugTackerManifest struct {
URL string `json:"url" toml:"url" validate:"omitempty,url"`
Contact string `json:"contact" toml:"contact" validate:"omitempty,email"`
}
// VersionManifest defines the version information in a module's manifest.
VersionManifest struct {
Repo string `json:"repo" toml:"repo" validate:"required,url"`
Documentation string `json:"documentation" toml:"documentation" validate:"omitempty,url"`
Version string `json:"version" toml:"version" validate:"required"`
SDKCompat string `json:"sdk_compat" toml:"sdk_compat"`
}
// Manifest defines a Cosmos SDK module manifest. It translates directly into
// a Module model.
Manifest struct {
Module ModuleManifest `json:"module" toml:"module"`
BugTracker BugTackerManifest `json:"bug_tracker" toml:"bug_tracker" validate:"omitempty,dive"`
Authors []AuthorsManifest `json:"authors" toml:"authors" validate:"required,gt=0,unique=Name,dive"`
Version VersionManifest `json:"version" toml:"version" validate:"required,dive"`
}
)
// Sanitizer defines a sanitization interface for cleaning HTML input.
type Sanitizer interface {
Sanitize(string) string
}
// ModuleFromManifest converts a Manifest to a Module model.
func ModuleFromManifest(manifest Manifest, sanitizer Sanitizer) models.Module {
authors := make([]models.User, len(manifest.Authors))
for i, a := range manifest.Authors {
authors[i] = models.User{Name: a.Name, Email: models.NewNullString(a.Email)}
}
keywords := make([]models.Keyword, len(manifest.Module.Keywords))
for i, k := range manifest.Module.Keywords {
keywords[i] = models.Keyword{Name: k}
}
bugTracker := models.BugTracker{
URL: models.NewNullString(sanitizer.Sanitize(manifest.BugTracker.URL)),
Contact: models.NewNullString(sanitizer.Sanitize(manifest.BugTracker.Contact)),
}
version := models.ModuleVersion{
Repo: sanitizer.Sanitize(manifest.Version.Repo),
Documentation: sanitizer.Sanitize(manifest.Version.Documentation),
Version: manifest.Version.Version,
SDKCompat: models.NewNullString(manifest.Version.SDKCompat),
}
return models.Module{
Name: manifest.Module.Name,
Description: sanitizer.Sanitize(manifest.Module.Description),
Homepage: sanitizer.Sanitize(manifest.Module.Homepage),
Version: version,
Authors: authors,
Keywords: keywords,
BugTracker: bugTracker,
}
}