-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.go
107 lines (96 loc) · 2.32 KB
/
stop.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package fptf
import (
"encoding/json"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)
// Stop is a single small point or structure at which vehicles stop.
// A Stop always belongs to a Station. It may for example be a sign,
// a basic shelter or a railway platform.
//
// If the underlying data source does not allow such a fine-grained
// distinction, use stations instead.
type Stop struct {
Id string
Name string
Station *Station
Location *Location
Meta interface{}
Partial bool // only show the id in the json response?
}
// used by marshal
type mStop struct {
Typed `bson:"inline"`
Id string `json:"id,omitempty" bson:"id,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Station *Station `json:"station,omitempty" bson:"station,omitempty"`
Location *Location `json:"location,omitempty" bson:"location,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"`
}
func (s *Stop) toM() *mStop {
return &mStop{
Typed: typedStop,
Id: s.Id,
Name: s.Name,
Station: s.Station,
Location: s.Location,
Meta: s.Meta,
}
}
func (s *Stop) fromM(m *mStop) {
s.Id = m.Id
s.Name = m.Name
s.Station = m.Station
s.Location = m.Location
s.Meta = m.Meta
}
// as it is optional to give either line id or Line object,
// we have to unmarshal|marshal it ourselves.
func (s *Stop) UnmarshalJSON(data []byte) error {
var id string
if err := json.Unmarshal(data, &id); err == nil {
s.Id = id
s.Partial = true
return nil
}
s.Partial = false
var m mStop
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
s.fromM(&m)
return nil
}
func (s *Stop) MarshalJSON() ([]byte, error) {
if s.Partial {
return json.Marshal(s.Id)
}
return json.Marshal(s.toM())
}
func (s *Stop) UnmarshalBSONValue(typ bsontype.Type, data []byte) error {
if typ == bson.TypeString {
var id string
err := bson.UnmarshalValue(bson.TypeString, data, &id)
if err != nil {
return err
}
s.Id = id
s.Partial = true
return nil
}
s.Partial = false
var m mStop
err := bson.Unmarshal(data, &m)
if err != nil {
return err
}
s.fromM(&m)
return nil
}
func (s *Stop) MarshalBSONValue() (bsontype.Type, []byte, error) {
if s.Partial {
return bson.MarshalValue(s.Id)
}
return bson.MarshalValue(s.toM())
}