-
Notifications
You must be signed in to change notification settings - Fork 9
/
parser.go
83 lines (72 loc) · 2.5 KB
/
parser.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package entity
import (
"context"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
// Calls one of the registered entityt parsers, repending on the value of the "urlCueKey" field.
func NewDispatchingEntityParser(urlCueKey string, parsers []EntityParser) *DispatchingEntityParser {
registeredParsers := map[string]EntityParser{}
for _, p := range parsers {
registeredParsers[p.Url()] = p
}
return &DispatchingEntityParser{
urlCueKey: urlCueKey,
registeredParsers: registeredParsers,
}
}
type DispatchingEntityParser struct {
urlCueKey string
// Key: url
registeredParsers map[string]EntityParser
}
type ParsedEntity struct {
Url string
Data proto.Message
}
func (p *DispatchingEntityParser) ParseEntity(ctx context.Context, env *schema.Environment, pl parsing.EarlyPackageLoader, loc pkggraph.Location, v *fncue.CueV) (ParsedEntity, error) {
// First checking for the full cueUrl
if cueUrl := v.LookupPath(p.urlCueKey); cueUrl.Exists() {
url, err := cueUrl.Val.String()
if err != nil {
return ParsedEntity{}, err
}
if i, ok := p.registeredParsers[url]; ok {
return parse(ctx, env, pl, loc, i, v)
} else {
return ParsedEntity{}, fnerrors.NewWithLocation(loc, "unknown url: %s", url)
}
}
// If the kind is not specified, trying the short form, e.g.:
// integration: golang {
// pkg: "."
// }
for _, p := range p.registeredParsers {
if shortV := v.LookupPath(p.Shortcut()); shortV.Exists() {
return parse(ctx, env, pl, loc, p, shortV)
}
// Shortest form:
// integration: "golang"
if str, err := v.Val.String(); err == nil && str == p.Shortcut() {
return parse(ctx, env, pl, loc, p, nil)
}
}
return ParsedEntity{}, fnerrors.NewWithLocation(loc, "%q content is not recognized, neither a full form nor a shorcut", v.Val.Path())
}
func parse(ctx context.Context, env *schema.Environment, pl parsing.EarlyPackageLoader, loc pkggraph.Location, p EntityParser, v *fncue.CueV) (ParsedEntity, error) {
data, err := p.Parse(ctx, env, pl, loc, v)
if err != nil {
return ParsedEntity{}, err
}
return ParsedEntity{
Url: p.Url(),
Data: data,
}, nil
}