forked from streamrail/vast-1
-
Notifications
You must be signed in to change notification settings - Fork 3
/
offset.go
40 lines (36 loc) · 978 Bytes
/
offset.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
package vast
import (
"fmt"
"strconv"
"strings"
)
// Offset represents either a vast.Duration or a percentage of the video duration.
type Offset struct {
// If not nil, the Offset is duration based
Duration *Duration
// If Duration is nil, the Offset is percent based
Percent float32
}
// MarshalText implements the encoding.TextMarshaler interface.
func (o Offset) MarshalText() ([]byte, error) {
if o.Duration != nil {
return o.Duration.MarshalText()
}
return []byte(fmt.Sprintf("%d%%", int(o.Percent*100))), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (o *Offset) UnmarshalText(data []byte) error {
if strings.HasSuffix(string(data), "%") {
p, err := strconv.ParseInt(string(data[:len(data)-1]), 10, 8)
if err != nil {
return fmt.Errorf("invalid offset: %s", data)
}
o.Percent = float32(p) / 100
return nil
} else {
o.Percent = 0
}
var d Duration
o.Duration = &d
return o.Duration.UnmarshalText(data)
}