-
Notifications
You must be signed in to change notification settings - Fork 117
/
yaml.go
69 lines (60 loc) · 1.63 KB
/
yaml.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
// Package yaml reads and writes artifacts that exactly mirror the internal representation
package yaml
import (
"context"
"errors"
"path/filepath"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/services/catalog/artifacts"
"gopkg.in/yaml.v3"
)
type artifact struct{}
var ErrNotSupported = errors.New("yaml only supported for sources and dashboards")
func init() {
artifacts.Register(".yaml", &artifact{})
}
func (r *artifact) DeSerialise(ctx context.Context, filePath, blob string, materializeDefault bool) (*drivers.CatalogEntry, error) {
dir := filepath.Base(filepath.Dir(filePath))
switch dir {
case "sources":
source := &Source{}
err := yaml.Unmarshal([]byte(blob), &source)
if err != nil {
return nil, err
}
return fromSourceArtifact(source, filePath)
case "dashboards":
metrics := &MetricsView{}
err := yaml.Unmarshal([]byte(blob), &metrics)
if err != nil {
return nil, err
}
return fromMetricsViewArtifact(metrics, filePath)
}
return nil, ErrNotSupported
}
func (r *artifact) Serialise(ctx context.Context, catalogObject *drivers.CatalogEntry) (string, error) {
switch catalogObject.Type {
case drivers.ObjectTypeSource:
source, err := toSourceArtifact(catalogObject)
if err != nil {
return "", err
}
out, err := yaml.Marshal(source)
if err != nil {
return "", err
}
return string(out), nil
case drivers.ObjectTypeMetricsView:
metrics, err := toMetricsViewArtifact(catalogObject)
if err != nil {
return "", err
}
out, err := yaml.Marshal(metrics)
if err != nil {
return "", err
}
return string(out), nil
}
return "", ErrNotSupported
}