-
Notifications
You must be signed in to change notification settings - Fork 0
/
transient.go
67 lines (52 loc) · 1.42 KB
/
transient.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
package roc
import (
"fmt"
"github.com/google/uuid"
proto "github.com/treethought/roc/proto/v1"
)
const EndpointTypeTransient string = "transient"
// TransientEndpoint is dynamically generated in-memory endpoint
// these are typically used for internal temporary resources.
type TransientEndpoint struct {
*BaseEndpoint
}
func NewTransientEndpoint(ed *proto.EndpointMeta) TransientEndpoint {
ed.Type = EndpointTypeTransient
if ed.GetIdentifier() == "" {
uid := uuid.New()
uri := fmt.Sprintf("transient://%s", uid.String())
ed.Identifier = uri
}
if ed.GetLiteral() == nil {
log.Warn("transient endpoint has nil represetation", "id", ed.Identifier)
}
if ed.GetGrammar() == nil {
ed.Grammar = &proto.Grammar{Base: ed.Identifier}
}
repr := Representation{ed.Literal}
log.Debug("creating transient endpoint",
"type", repr.Type(),
"uri", ed.Identifier,
)
log.Trace(repr.String())
return TransientEndpoint{
BaseEndpoint: NewBaseEndpoint(ed),
}
}
func (e TransientEndpoint) Type() string {
return EndpointTypeTransient
}
func (e TransientEndpoint) Source(ctx *RequestContext) interface{} {
log.Debug("sourcing transient endpoint",
"identifier", ctx.Request().Identifier(),
"type", e.Type(),
)
rep := NewRepresentation(e.Meta().GetLiteral())
log.Trace(rep.String())
m, err := rep.ToMessage()
if err != nil {
log.Error("failed to construct transient message", "err", err)
return err
}
return m
}