-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.go
More file actions
174 lines (152 loc) · 4.27 KB
/
configuration.go
File metadata and controls
174 lines (152 loc) · 4.27 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"os"
"path"
"strconv"
"strings"
toolparameters "codacy.com/codacy-gorevive/toolparameters"
codacy "github.com/codacy/codacy-engine-golang-seed/v6"
)
const (
unnamedParamName = "unnamedParam"
sourceConfigFileName = "revive.toml"
)
// paramValueByType checks the type of parameter according to the tool documentation
func paramValueByType(paramValue interface{}, ruleDefinition toolparameters.RuleParameter) interface{} {
switch ruleDefinition.Type {
case toolparameters.ListType:
// make sure it's string before splitting
if s, ok := paramValue.(string); ok {
return strings.Split(s, ", ")
}
return []string{}
case toolparameters.IntType:
switch v := paramValue.(type) {
case int:
return v
case float64:
return int(v)
case string:
if i, err := strconv.Atoi(v); err == nil {
return i
}
return 0
default:
return 0
}
case toolparameters.FloatType:
switch v := paramValue.(type) {
case float64:
return v
case int:
return float64(v)
case string:
if f, err := strconv.ParseFloat(v, 64); err == nil {
return f
}
return 0.0
default:
return 0.0
}
case toolparameters.StringType:
return fmt.Sprintf("%v", paramValue)
default:
return paramValue
}
}
// paramValue converts codacy's parameter into a revive parameter value
func paramValue(param codacy.PatternParameter, patternID string) interface{} {
ruleDefinition, notFound := toolparameters.FindRuleParameterDefinition(patternID)
if (param.Value == "" || param.Value == nil) && notFound == nil {
param.Value = ruleDefinition.Default
}
if notFound != nil {
if isInteger(param.Value) {
return int(param.Value.(float64))
}
}
// This is to handle rules with sub-parameters
for _, p := range ruleDefinition.Parameters {
if p.Name == param.Name {
return paramValueByType(param.Value, p)
}
}
return paramValueByType(param.Value, ruleDefinition)
}
func unnamedParam(value interface{}) []interface{} {
resultTmp := []interface{}{}
switch value.(type) {
case []string:
// if is a []string, append all values to res, one by one
for _, v := range value.([]string) {
resultTmp = append(resultTmp, v)
}
default:
resultTmp = append(resultTmp, value)
}
return resultTmp
}
// patternParametersAsReviveValues converts pattern parameters into a list of revive arguments
func patternParametersAsReviveValues(pattern codacy.Pattern) []interface{} {
namedParameters := map[string]interface{}{}
for _, p := range pattern.Parameters {
value := paramValue(p, pattern.ID)
if p.Name == unnamedParamName {
return unnamedParam(value)
}
namedParameters[p.Name] = value
}
if len(namedParameters) > 0 {
return []interface{}{
namedParameters,
}
}
return []interface{}{}
}
func reviveArguments(paramsValues []interface{}) map[string]interface{} {
if paramsValues == nil || len(paramsValues) == 0 {
return map[string]interface{}{}
}
return map[string]interface{}{
"arguments": paramsValues,
}
}
func patternsToReviveConfigMap(patterns []codacy.Pattern) map[string]interface{} {
patternsMap := map[string]interface{}{}
for _, pattern := range patterns {
patternsMap[pattern.ID] = reviveArguments(patternParametersAsReviveValues(pattern))
}
rules := map[string]interface{}{
"rule": patternsMap,
}
return rules
}
func generateToolConfigurationContent(patterns []codacy.Pattern) string {
patternsMap := patternsToReviveConfigMap(patterns)
tomlString, err := mapToTOML(patternsMap)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return tomlString
}
func configurationFromSourceCode(sourceFolder string) (string, error) {
filename := path.Join(sourceFolder, sourceConfigFileName)
contentByte, err := os.ReadFile(filename)
return string(contentByte), err
}
// getConfigurationFile returns file, boolean saying if it is temp and error
func getConfigurationFile(patterns *[]codacy.Pattern, sourceFolder string) (*os.File, error) {
// if no patterns, try to use configuration from source code
// otherwise default configuration file
if patterns == nil {
sourceConfigFileContent, err := configurationFromSourceCode(sourceFolder)
if err == nil {
return writeToTempFile(sourceConfigFileContent)
}
return nil, err
}
content := generateToolConfigurationContent(*patterns)
return writeToTempFile(content)
}