Skip to content

Commit 0ac15b5

Browse files
authored
Merge pull request #55 from smacker/read_config
Read config
2 parents 8828b53 + b3ee2b0 commit 0ac15b5

File tree

7 files changed

+406
-36
lines changed

7 files changed

+406
-36
lines changed

cmd/lookout/push.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ func (c *PushCommand) Execute(args []string) error {
4646
return err
4747
}
4848

49-
srv := lookout.NewServer(nil, &LogPoster{log.DefaultLogger}, nil, map[string]lookout.AnalyzerClient{
50-
"test-analyzes": client,
49+
srv := lookout.NewServer(nil, &LogPoster{log.DefaultLogger}, dataSrv.FileGetter, map[string]lookout.Analyzer{
50+
"test-analyzes": lookout.Analyzer{
51+
Client: client,
52+
},
5153
})
5254

5355
log, err := c.repo.Log(&gogit.LogOptions{From: plumbing.NewHash(toRef.Hash)})

cmd/lookout/review.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ func (c *ReviewCommand) Execute(args []string) error {
4444
return err
4545
}
4646

47-
srv := lookout.NewServer(nil, &LogPoster{log.DefaultLogger}, nil, map[string]lookout.AnalyzerClient{
48-
"test-analyzes": client,
47+
srv := lookout.NewServer(nil, &LogPoster{log.DefaultLogger}, dataSrv.FileGetter, map[string]lookout.Analyzer{
48+
"test-analyzes": lookout.Analyzer{
49+
Client: client,
50+
},
4951
})
5052

5153
err = srv.HandleReview(context.TODO(), &lookout.ReviewEvent{

cmd/lookout/serve.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ func init() {
2424
}
2525
}
2626

27-
// AnalyzerConfig is a configuration of analyzer
28-
type AnalyzerConfig struct {
29-
Name string
30-
Addr string
31-
}
32-
33-
// Config is a server configuration
34-
type Config struct {
35-
Analyzers []AnalyzerConfig
36-
}
37-
3827
type ServeCommand struct {
3928
ConfigFile string `long:"config" short:"c" default:"config.yml" env:"LOOKOUT_CONFIG_FILE" description:"path to configuration file"`
4029
GithubUser string `long:"github-user" env:"GITHUB_USER" description:"user for the GitHub API"`
@@ -51,7 +40,7 @@ type ServeCommand struct {
5140
}
5241

5342
func (c *ServeCommand) Execute(args []string) error {
54-
var conf Config
43+
var conf lookout.ServerConfig
5544
configData, err := ioutil.ReadFile(c.ConfigFile)
5645
if err != nil {
5746
return fmt.Errorf("Can't open configuration file: %s", err)
@@ -71,13 +60,16 @@ func (c *ServeCommand) Execute(args []string) error {
7160
return err
7261
}
7362

74-
analyzers := make(map[string]lookout.AnalyzerClient, len(conf.Analyzers))
63+
analyzers := make(map[string]lookout.Analyzer, len(conf.Analyzers))
7564
for _, aConf := range conf.Analyzers {
76-
a, err := c.startAnalyzer(aConf)
65+
client, err := c.startAnalyzer(aConf)
7766
if err != nil {
7867
return err
7968
}
80-
analyzers[aConf.Name] = a
69+
analyzers[aConf.Name] = lookout.Analyzer{
70+
Client: client,
71+
Config: aConf,
72+
}
8173
}
8274

8375
poster, err := c.initPoster()
@@ -113,7 +105,7 @@ func (c *ServeCommand) initPoster() (lookout.Poster, error) {
113105
}), nil
114106
}
115107

116-
func (c *ServeCommand) startAnalyzer(conf AnalyzerConfig) (lookout.AnalyzerClient, error) {
108+
func (c *ServeCommand) startAnalyzer(conf lookout.AnalyzerConfig) (lookout.AnalyzerClient, error) {
117109
addr, err := lookout.ToGoGrpcAddress(conf.Addr)
118110
if err != nil {
119111
return nil, err

pb/struct.go

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package pb
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
types "github.com/gogo/protobuf/types"
8+
)
9+
10+
// ToStruct converts a map[string]interface{} to a types.Struct
11+
func ToStruct(v map[string]interface{}) *types.Struct {
12+
size := len(v)
13+
if size == 0 {
14+
return nil
15+
}
16+
17+
fields := make(map[string]*types.Value, size)
18+
for k, v := range v {
19+
fields[k] = ToValue(v)
20+
}
21+
22+
return &types.Struct{
23+
Fields: fields,
24+
}
25+
}
26+
27+
func newBoolValue(v bool) *types.Value {
28+
return &types.Value{
29+
Kind: &types.Value_BoolValue{
30+
BoolValue: v,
31+
},
32+
}
33+
}
34+
35+
func newNumberValue(v float64) *types.Value {
36+
return &types.Value{
37+
Kind: &types.Value_NumberValue{
38+
NumberValue: v,
39+
},
40+
}
41+
}
42+
43+
// ToValue converts an interface{} to a types.Value
44+
func ToValue(v interface{}) *types.Value {
45+
switch v := v.(type) {
46+
case nil:
47+
return nil
48+
case bool:
49+
return newBoolValue(v)
50+
case int:
51+
return newNumberValue(float64(v))
52+
case int8:
53+
return newNumberValue(float64(v))
54+
case int32:
55+
return newNumberValue(float64(v))
56+
case int64:
57+
return newNumberValue(float64(v))
58+
case uint:
59+
return newNumberValue(float64(v))
60+
case uint8:
61+
return newNumberValue(float64(v))
62+
case uint32:
63+
return newNumberValue(float64(v))
64+
case uint64:
65+
return newNumberValue(float64(v))
66+
case float32:
67+
return newNumberValue(float64(v))
68+
case float64:
69+
return newNumberValue(float64(v))
70+
case string:
71+
return &types.Value{
72+
Kind: &types.Value_StringValue{
73+
StringValue: v,
74+
},
75+
}
76+
default:
77+
return toValue(reflect.ValueOf(v))
78+
}
79+
}
80+
81+
func toValue(v reflect.Value) *types.Value {
82+
switch v.Kind() {
83+
case reflect.Bool:
84+
return newBoolValue(v.Bool())
85+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
86+
return newNumberValue(float64(v.Int()))
87+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
88+
return newNumberValue(float64(v.Uint()))
89+
case reflect.Float32, reflect.Float64:
90+
return newNumberValue(v.Float())
91+
case reflect.Ptr:
92+
if v.IsNil() {
93+
return nil
94+
}
95+
return toValue(reflect.Indirect(v))
96+
case reflect.Array, reflect.Slice:
97+
size := v.Len()
98+
if size == 0 {
99+
return nil
100+
}
101+
values := make([]*types.Value, size)
102+
for i := 0; i < size; i++ {
103+
values[i] = toValue(v.Index(i))
104+
}
105+
return &types.Value{
106+
Kind: &types.Value_ListValue{
107+
ListValue: &types.ListValue{
108+
Values: values,
109+
},
110+
},
111+
}
112+
case reflect.Struct:
113+
t := v.Type()
114+
size := v.NumField()
115+
if size == 0 {
116+
return nil
117+
}
118+
fields := make(map[string]*types.Value, size)
119+
for i := 0; i < size; i++ {
120+
name := t.Field(i).Name
121+
if len(name) > 0 {
122+
fields[name] = toValue(v.Field(i))
123+
}
124+
}
125+
if len(fields) == 0 {
126+
return nil
127+
}
128+
return &types.Value{
129+
Kind: &types.Value_StructValue{
130+
StructValue: &types.Struct{
131+
Fields: fields,
132+
},
133+
},
134+
}
135+
case reflect.Map:
136+
keys := v.MapKeys()
137+
if len(keys) == 0 {
138+
return nil
139+
}
140+
fields := make(map[string]*types.Value, len(keys))
141+
for _, k := range keys {
142+
if k.Kind() == reflect.String {
143+
fields[k.String()] = toValue(v.MapIndex(k))
144+
}
145+
}
146+
if len(fields) == 0 {
147+
return nil
148+
}
149+
return &types.Value{
150+
Kind: &types.Value_StructValue{
151+
StructValue: &types.Struct{
152+
Fields: fields,
153+
},
154+
},
155+
}
156+
default:
157+
return &types.Value{
158+
Kind: &types.Value_StringValue{
159+
StringValue: fmt.Sprint(v),
160+
},
161+
}
162+
}
163+
}

pb/struct_test.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package pb_test
2+
3+
import (
4+
"testing"
5+
6+
types "github.com/gogo/protobuf/types"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/src-d/lookout/pb"
10+
)
11+
12+
func TestToStruct(t *testing.T) {
13+
require := require.New(t)
14+
15+
inputMap := map[string]interface{}{
16+
"bool": true,
17+
"int": 1,
18+
"string": "val",
19+
"float": 0.5,
20+
"nil": nil,
21+
"array": []string{"val1", "val2"},
22+
"map": map[string]int{
23+
"field1": 1,
24+
},
25+
"struct": struct {
26+
Val string
27+
}{Val: "val"},
28+
}
29+
30+
expectedSt := &types.Struct{
31+
Fields: map[string]*types.Value{
32+
"bool": &types.Value{
33+
Kind: &types.Value_BoolValue{
34+
BoolValue: true,
35+
},
36+
},
37+
"int": &types.Value{
38+
Kind: &types.Value_NumberValue{
39+
NumberValue: 1,
40+
},
41+
},
42+
"string": &types.Value{
43+
Kind: &types.Value_StringValue{
44+
StringValue: "val",
45+
},
46+
},
47+
"float": &types.Value{
48+
Kind: &types.Value_NumberValue{
49+
NumberValue: 0.5,
50+
},
51+
},
52+
"nil": nil,
53+
"array": &types.Value{
54+
Kind: &types.Value_ListValue{
55+
ListValue: &types.ListValue{
56+
Values: []*types.Value{
57+
&types.Value{
58+
Kind: &types.Value_StringValue{
59+
StringValue: "val1",
60+
},
61+
},
62+
&types.Value{
63+
Kind: &types.Value_StringValue{
64+
StringValue: "val2",
65+
},
66+
},
67+
},
68+
},
69+
},
70+
},
71+
"map": &types.Value{
72+
Kind: &types.Value_StructValue{
73+
StructValue: &types.Struct{
74+
Fields: map[string]*types.Value{
75+
"field1": &types.Value{
76+
Kind: &types.Value_NumberValue{
77+
NumberValue: 1,
78+
},
79+
},
80+
},
81+
},
82+
},
83+
},
84+
"struct": &types.Value{
85+
Kind: &types.Value_StructValue{
86+
StructValue: &types.Struct{
87+
Fields: map[string]*types.Value{
88+
"Val": &types.Value{
89+
Kind: &types.Value_StringValue{
90+
StringValue: "val",
91+
},
92+
},
93+
},
94+
},
95+
},
96+
},
97+
},
98+
}
99+
100+
st := pb.ToStruct(inputMap)
101+
require.Equal(expectedSt, st)
102+
}

0 commit comments

Comments
 (0)