-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtaggers.go
50 lines (41 loc) · 981 Bytes
/
taggers.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
package sqlcomment
import (
"context"
"fmt"
"runtime/debug"
)
// driverVersionTagger adds `db_driver` tag with "ent:<version>"
type driverVersionTagger struct {
version string
}
func NewDriverVersionTagger() driverVersionTagger {
info, ok := debug.ReadBuildInfo()
if !ok {
return driverVersionTagger{"ent"}
}
for _, d := range info.Deps {
if d.Path == "entgo.io/ent" {
return driverVersionTagger{fmt.Sprintf("ent:%s", d.Version)}
}
}
return driverVersionTagger{"ent"}
}
func (dv driverVersionTagger) Tag(ctx context.Context) Tags {
return Tags{
KeyDBDriver: dv.version,
}
}
type contextTagger struct{}
func (ct contextTagger) Tag(ctx context.Context) Tags {
return FromContext(ctx)
}
type staticTagger struct {
tags Tags
}
// NewStaticTagger returns an Tagger which adds tags to every SQL comment.
func NewStaticTagger(tags Tags) staticTagger {
return staticTagger{tags}
}
func (st staticTagger) Tag(ctx context.Context) Tags {
return st.tags
}