-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathconstraints.go
More file actions
79 lines (69 loc) · 2.04 KB
/
Copy pathconstraints.go
File metadata and controls
79 lines (69 loc) · 2.04 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
package models
import (
"time"
"github.com/golang/geo/s2"
"github.com/golang/protobuf/ptypes"
"github.com/interuss/dss/pkg/api/v1/scdpb"
dsserr "github.com/interuss/dss/pkg/errors"
dssmodels "github.com/interuss/dss/pkg/models"
"github.com/interuss/stacktrace"
)
// Constraint models a constraint, as known by the DSS
type Constraint struct {
ID dssmodels.ID
Version Version
OVN OVN
Owner dssmodels.Owner
StartTime *time.Time
EndTime *time.Time
AltitudeLower *float32
AltitudeUpper *float32
USSBaseURL string
Cells s2.CellUnion
}
// ToProto converts the Constraint to its proto API format
func (c *Constraint) ToProto() (*scdpb.ConstraintReference, error) {
result := &scdpb.ConstraintReference{
Id: c.ID.String(),
Ovn: c.OVN.String(),
Owner: c.Owner.String(),
Version: int32(c.Version),
UssBaseUrl: c.USSBaseURL,
}
if c.StartTime != nil {
ts, err := ptypes.TimestampProto(*c.StartTime)
if err != nil {
return nil, stacktrace.Propagate(err, "Error converting start time to proto")
}
result.TimeStart = &scdpb.Time{
Value: ts,
Format: dssmodels.TimeFormatRFC3339,
}
}
if c.EndTime != nil {
ts, err := ptypes.TimestampProto(*c.EndTime)
if err != nil {
return nil, stacktrace.Propagate(err, "Error converting end time to proto")
}
result.TimeEnd = &scdpb.Time{
Value: ts,
Format: dssmodels.TimeFormatRFC3339,
}
}
return result, nil
}
// ValidateTimeRange validates the time range of c.
func (c *Constraint) ValidateTimeRange() error {
if c.StartTime == nil {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Constraint must have an time_start")
}
// EndTime cannot be omitted for new Constraints.
if c.EndTime == nil {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Constraint must have an time_end")
}
// EndTime cannot be before StartTime.
if c.EndTime.Sub(*c.StartTime) < 0 {
return stacktrace.NewErrorWithCode(dsserr.BadRequest, "Constraint time_end must be after time_start")
}
return nil
}