-
Notifications
You must be signed in to change notification settings - Fork 6
/
image.go
54 lines (46 loc) · 1.14 KB
/
image.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
package fragment
import (
"fmt"
"reflect"
"github.com/SoCloz/goprismic/fragment/image"
"github.com/SoCloz/goprismic/fragment/link"
)
// An image, with multiple views
type Image struct {
Main image.View `json:"main"`
Views map[string]image.View `json:"views"`
}
// Returns a view of this image
func (i *Image) GetView(view string) (*image.View, bool) {
v, found := i.Views[view]
return &v, found
}
func (i *Image) Decode(_ string, enc interface{}) error {
dec, ok := enc.(map[string]interface{})
if !ok {
return fmt.Errorf("%+v is not a map", enc)
}
if v, found := dec["main"]; found {
(&i.Main).Decode(v)
}
if v, found := dec["views"]; found {
views, ok := v.(map[string]interface{})
if !ok {
return fmt.Errorf("unable to decode image fragment : %+v is a %s, not a map", enc, reflect.TypeOf(enc))
}
i.Views = make(map[string]image.View)
for k, view := range views {
iv := &image.View{}
iv.Decode(view)
i.Views[k] = *iv
}
}
return nil
}
func (i *Image) AsText() string {
return i.Main.AsText()
}
func (i *Image) AsHtml() string {
return i.Main.AsHtml()
}
func (i *Image) ResolveLinks(_ link.Resolver) {}