-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
passagedata.go
42 lines (37 loc) · 1.05 KB
/
passagedata.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
/*
Copyright © 2014–2020 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
"encoding/json"
)
type passageMetadataJSON struct {
Position string `json:"position,omitempty"` // Twine 2 (`position`) & Twine 1 (`twine-position`).
Size string `json:"size,omitempty"` // Twine 2 (`size`).
}
func (p *passage) marshalMetadata() []byte {
marshaled, err := json.Marshal(&passageMetadataJSON{
p.metadata.position,
p.metadata.size,
})
if err != nil {
// NOTE: We should never be able to see an error here. If we do,
// then something truly exceptional—in a bad way—has happened, so
// we get our panic on.
panic(err)
}
return marshaled
}
func (p *passage) unmarshalMetadata(marshaled []byte) error {
metadata := passageMetadataJSON{}
if err := json.Unmarshal(marshaled, &metadata); err != nil {
return err
}
p.metadata = &passageMetadata{
position: metadata.Position,
size: metadata.Size,
}
return nil
}