Repository navigation
Expand file tree
/
Copy pathparse.go
More file actions
137 lines (123 loc) · 4.24 KB
/
Copy pathparse.go
File metadata and controls
137 lines (123 loc) · 4.24 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package smapiweb
import (
"encoding/json"
"errors"
"strings"
)
// unmarshalLenient is json.Unmarshal that keeps what decoded when an object's field has the wrong type; syntax
// errors, a value of the wrong shape for v itself and an array element that is not an object are still errors.
func unmarshalLenient(b []byte, v any) error {
err := json.Unmarshal(b, v)
if te, ok := errors.AsType[*json.UnmarshalTypeError](err); ok && atStructField(te.Field) {
return nil
}
return err
}
// atStructField reports whether the type error names a field inside an object. A path whose last segment is an
// array index names the element itself, so nothing of that record decoded to keep.
func atStructField(path string) bool {
last := path[strings.LastIndexByte(path, '.')+1:]
return strings.ContainsFunc(last, func(r rune) bool { return r < '0' || r > '9' })
}
// text accepts a string, a number (kept as written) or anything else (empty).
type text string
func (t *text) UnmarshalJSON(b []byte) error {
var s string
if json.Unmarshal(b, &s) == nil {
*t = text(strings.TrimSpace(s))
return nil
}
var n json.Number
if json.Unmarshal(b, &n) == nil {
*t = text(n)
}
return nil
}
// list keeps the string entries of an array and ignores a value of any other shape.
type list []text
func (l *list) UnmarshalJSON(b []byte) error {
var items []text
if json.Unmarshal(b, &items) == nil {
*l = items
}
return nil
}
// flexBool accepts a JSON boolean or the strings "true"/"false", as SMAPI's Json.NET reader does; anything
// else reads as true, the default for IsRequired.
type flexBool bool
func (b *flexBool) UnmarshalJSON(data []byte) error {
var v bool
if json.Unmarshal(data, &v) == nil {
*b = flexBool(v)
return nil
}
var s string
_ = json.Unmarshal(data, &s)
*b = flexBool(!strings.EqualFold(strings.TrimSpace(s), "false"))
return nil
}
type rawDependency struct {
UniqueID text `json:"UniqueID"`
MinimumVersion text `json:"MinimumVersion"`
IsRequired *flexBool `json:"IsRequired"`
}
type rawManifest struct {
Name text `json:"Name"`
Version text `json:"Version"`
UniqueID text `json:"UniqueID"`
UpdateKeys list `json:"UpdateKeys"`
Dependencies []rawDependency `json:"Dependencies"`
ContentPackFor *rawDependency `json:"ContentPackFor"`
}
type rawPage struct {
ID int64 `json:"ID"`
Name text `json:"Name"`
Author text `json:"Author"`
PageURL text `json:"PageURL"`
Version text `json:"Version"`
Downloads []struct {
ID int64 `json:"ID"`
Type text `json:"Type"`
Version text `json:"Version"`
FileName text `json:"FileName"`
SizeInBytes int64 `json:"SizeInBytes"`
Mods []struct {
Manifest *rawManifest `json:"Manifest"`
} `json:"Mods"`
} `json:"Downloads"`
}
// parsePage reads a dataset page entry, ignoring unknown fields and tolerating fields of the wrong type
// (which json reports as an error after decoding the rest, so the partial result is kept).
func parsePage(b []byte) (Page, error) {
var r rawPage
if err := unmarshalLenient(b, &r); err != nil {
return Page{}, err
}
p := Page{ID: int(r.ID), Name: string(r.Name), Author: string(r.Author), PageURL: webURL(string(r.PageURL)), Version: string(r.Version)}
for _, d := range r.Downloads {
f := File{ID: d.ID, Type: string(d.Type), Version: string(d.Version), FileName: string(d.FileName), SizeInBytes: d.SizeInBytes}
for _, m := range d.Mods {
if m.Manifest == nil || m.Manifest.UniqueID == "" {
continue
}
f.Mods = append(f.Mods, m.Manifest.mod())
}
p.Downloads = append(p.Downloads, f)
}
return p, nil
}
func (m *rawManifest) mod() Mod {
out := Mod{UniqueID: string(m.UniqueID), Name: string(m.Name), Version: string(m.Version)}
for _, k := range m.UpdateKeys {
out.UpdateKeys = append(out.UpdateKeys, string(k))
}
for _, d := range m.Dependencies {
if d.UniqueID != "" {
out.Dependencies = append(out.Dependencies, Dependency{string(d.UniqueID), string(d.MinimumVersion), d.IsRequired == nil || bool(*d.IsRequired)})
}
}
if cp := m.ContentPackFor; cp != nil && cp.UniqueID != "" {
out.Dependencies = append(out.Dependencies, Dependency{string(cp.UniqueID), string(cp.MinimumVersion), true})
}
return out
}