generated from things-labs/cicd-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.go
71 lines (64 loc) · 1.72 KB
/
proc.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
package main
import (
"strings"
"github.com/things-go/proc/proc"
"google.golang.org/protobuf/compiler/protogen"
)
// annotation const value
const (
Identity = "errno"
Attribute_Name_Status = "status"
Attribute_Name_Code = "code"
Attribute_Name_Message = "message"
)
type ErrnoDerive struct {
Enabled bool
Status int
}
func ParseDeriveErrno(s protogen.Comments) (*ErrnoDerive, proc.CommentLines) {
derives, remainComments := proc.NewCommentLines(s.String()).FindDerives(Identity)
ds := proc.Derives(derives)
ret := &ErrnoDerive{
Enabled: ds.ContainHeadless(Identity),
Status: 500,
}
values := ds.FindValue(Identity, Attribute_Name_Status)
for _, value := range values {
if v, ok := value.(proc.Integer); ok && v.Value > 0 && v.Value < 1000 {
ret.Status = int(v.Value)
}
}
return ret, remainComments
}
type ErrnoValueDerive struct {
Status int
Code int
Message string
}
func ParseDeriveErrnoValue(status, code int, s protogen.Comments) (*ErrnoValueDerive, proc.CommentLines) {
derives, remainComments := proc.NewCommentLines(s.String()).FindDerives(Identity)
ret := &ErrnoValueDerive{
Status: status,
Code: code,
Message: strings.ReplaceAll(strings.ReplaceAll(strings.TrimSpace(remainComments.LineString()), "\n", ","), `"`, `\"`),
}
for _, d := range derives {
for _, v := range d.Attrs {
switch v.Name {
case Attribute_Name_Status:
if v, ok := v.Value.(proc.Integer); ok {
ret.Status = int(v.Value)
}
case Attribute_Name_Code:
if v, ok := v.Value.(proc.Integer); ok {
ret.Code = int(v.Value)
}
case Attribute_Name_Message:
if v, ok := v.Value.(proc.String); ok {
ret.Message = v.Value
}
}
}
}
return ret, remainComments
}