Skip to content

Commit 85f7a04

Browse files
committed
style(go): apply gofmt to all files
1 parent 8adf813 commit 85f7a04

File tree

4 files changed

+80
-85
lines changed

4 files changed

+80
-85
lines changed

go/compiler.go

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ func RelaxedReSyntax(yes bool) CompileOption {
7575
// ErrorOnSlowPattern is an option for [NewCompiler] and [Compile] that
7676
// tells the compiler to treat slow patterns as errors instead of warnings.
7777
func ErrorOnSlowPattern(yes bool) CompileOption {
78-
return func(c *Compiler) error {
79-
c.errorOnSlowPattern = yes
80-
return nil
81-
}
78+
return func(c *Compiler) error {
79+
c.errorOnSlowPattern = yes
80+
return nil
81+
}
8282
}
8383

8484
// A structure that contains the options passed to [Compiler.AddSource].
@@ -97,64 +97,63 @@ type SourceOption func(opt *sourceOptions) error
9797
// source's origin. This origin appears in error reports, for instance, if
9898
// if origin is "some_file.yar", error reports will look like:
9999
//
100-
// error: syntax error
101-
// --> some_file.yar:4:17
102-
// |
103-
// 4 | ... more details
104-
//
100+
// error: syntax error
101+
// --> some_file.yar:4:17
102+
// |
103+
// 4 | ... more details
105104
//
106105
// Example:
107106
//
108-
// c := NewCompiler()
109-
// c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
107+
// c := NewCompiler()
108+
// c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
110109
func WithOrigin(origin string) SourceOption {
111-
return func(opts *sourceOptions) error {
112-
opts.origin = origin
113-
return nil
114-
}
110+
return func(opts *sourceOptions) error {
111+
opts.origin = origin
112+
return nil
113+
}
115114
}
116115

117116
// CompileError represents each of the errors returned by [Compiler.Errors].
118117
type CompileError struct {
119118
// Error code (e.g: "E001").
120-
Code string
119+
Code string `json:"code"`
121120
// Error title (e.g: "unknown identifier `foo`").
122-
Title string
121+
Title string `json:"title"`
123122
// Each of the labels in the error report.
124-
Labels []Label
123+
Labels []Label `json:"labels"`
125124
// The error's full report, as shown by the command-line tool.
126-
Text string
125+
Text string `json:"text"`
127126
}
128127

129128
// Warning represents each of the warnings returned by [Compiler.Warnings].
130129
type Warning struct {
131130
// Error code (e.g: "slow_pattern").
132-
Code string
131+
Code string `json:"code"`
133132
// Error title (e.g: "slow pattern").
134-
Title string
133+
Title string `json:"title"`
135134
// Each of the labels in the error report.
136-
Labels []Label
135+
Labels []Label `json:"labels"`
137136
// The error's full report, as shown by the command-line tool.
138-
Text string
137+
Text string `json:"text"`
139138
}
140139

141140
// Label represents a label in a [CompileError].
142141
type Label struct {
143142
// Label's level (e.g: "error", "warning", "info", "note", "help").
144-
Level string
143+
Level string `json:"level"`
145144
// Origin of the code where the error occurred.
146-
CodeOrigin string
145+
CodeOrigin string `json:"code_origin"`
147146
// The code span highlighted by this label.
148-
Span Span
147+
Span Span `json:"span"`
149148
// Text associated to the label.
150-
Text string
149+
Text string `json:"text"`
151150
}
152151

153152
// Span represents the starting and ending point of some piece of source
154153
// code.
155154
type Span struct {
156-
Start int
157-
End int
155+
Start int `json:"start"`
156+
End int `json:"end"`
158157
}
159158

160159
// Error returns the error's full report.
@@ -164,18 +163,18 @@ func (c CompileError) Error() string {
164163

165164
// Compiler represent a YARA compiler.
166165
type Compiler struct {
167-
cCompiler *C.YRX_COMPILER
168-
relaxedReSyntax bool
166+
cCompiler *C.YRX_COMPILER
167+
relaxedReSyntax bool
169168
errorOnSlowPattern bool
170-
ignoredModules map[string]bool
171-
vars map[string]interface{}
169+
ignoredModules map[string]bool
170+
vars map[string]interface{}
172171
}
173172

174173
// NewCompiler creates a new compiler.
175-
func NewCompiler(opts... CompileOption) (*Compiler, error) {
174+
func NewCompiler(opts ...CompileOption) (*Compiler, error) {
176175
c := &Compiler{
177176
ignoredModules: make(map[string]bool),
178-
vars: make(map[string]interface{}),
177+
vars: make(map[string]interface{}),
179178
}
180179

181180
for _, opt := range opts {
@@ -235,11 +234,11 @@ func (c *Compiler) initialize() error {
235234
//
236235
// Examples:
237236
//
238-
// c := NewCompiler()
239-
// c.AddSource("rule foo { condition: true }")
240-
// c.AddSource("rule bar { condition: true }")
241-
// c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
242-
func (c *Compiler) AddSource(src string, opts... SourceOption) error {
237+
// c := NewCompiler()
238+
// c.AddSource("rule foo { condition: true }")
239+
// c.AddSource("rule bar { condition: true }")
240+
// c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
241+
func (c *Compiler) AddSource(src string, opts ...SourceOption) error {
243242
options := &sourceOptions{}
244243
for _, opt := range opts {
245244
opt(options)
@@ -249,10 +248,10 @@ func (c *Compiler) AddSource(src string, opts... SourceOption) error {
249248
defer C.free(unsafe.Pointer(cSrc))
250249

251250
var cOrigin *C.char
252-
if options.origin != "" {
253-
cOrigin = C.CString(options.origin)
254-
defer C.free(unsafe.Pointer(cOrigin))
255-
}
251+
if options.origin != "" {
252+
cOrigin = C.CString(options.origin)
253+
defer C.free(unsafe.Pointer(cOrigin))
254+
}
256255

257256
// The call to runtime.LockOSThread() is necessary to make sure that
258257
// yrx_compiler_add_source and yrx_last_error are called from the same OS
@@ -295,16 +294,16 @@ func (c *Compiler) ignoreModule(module string) {
295294
//
296295
// Examples:
297296
//
298-
// c := NewCompiler()
299-
// // Add some rule named "foo" under the default namespace
300-
// c.AddSource("rule foo { condition: true }")
297+
// c := NewCompiler()
298+
// // Add some rule named "foo" under the default namespace
299+
// c.AddSource("rule foo { condition: true }")
301300
//
302-
// // Create a new namespace named "bar"
303-
// c.NewNamespace("bar")
301+
// // Create a new namespace named "bar"
302+
// c.NewNamespace("bar")
304303
//
305-
// // It's ok to add another rule named "foo", as it is in a different
306-
// // namespace than the previous one.
307-
// c.AddSource("rule foo { condition: true }")
304+
// // It's ok to add another rule named "foo", as it is in a different
305+
// // namespace than the previous one.
306+
// c.AddSource("rule foo { condition: true }")
308307
func (c *Compiler) NewNamespace(namespace string) {
309308
cNamespace := C.CString(namespace)
310309
defer C.free(unsafe.Pointer(cNamespace))

go/compiler_test.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func TestRelaxedReSyntax(t *testing.T) {
3939
assert.Len(t, scanResults.MatchingRules(), 1)
4040
}
4141

42-
4342
func TestErrorOnSlowPattern(t *testing.T) {
4443
_, err := Compile(`
4544
rule test { strings: $a = /a.*/ condition: $a }`,
@@ -107,7 +106,7 @@ func TestVariables(t *testing.T) {
107106

108107
func TestError(t *testing.T) {
109108
_, err := Compile("rule test { condition: foo }")
110-
expected := `error[E009]: unknown identifier `+"`foo`"+`
109+
expected := `error[E009]: unknown identifier ` + "`foo`" + `
111110
--> line:1:24
112111
|
113112
1 | rule test { condition: foo }
@@ -116,7 +115,6 @@ func TestError(t *testing.T) {
116115
assert.EqualError(t, err, expected)
117116
}
118117

119-
120118
func TestErrors(t *testing.T) {
121119
c, err := NewCompiler()
122120
assert.NoError(t, err)
@@ -127,17 +125,17 @@ func TestErrors(t *testing.T) {
127125
c.AddSource("rule test_2 { condition: foo }", WithOrigin("test.yar"))
128126
assert.Equal(t, []CompileError{
129127
{
130-
Code: "E009",
128+
Code: "E009",
131129
Title: "unknown identifier `foo`",
132130
Labels: []Label{
133131
{
134-
Level: "error",
132+
Level: "error",
135133
CodeOrigin: "",
136-
Span: Span { Start: 25, End: 28 },
137-
Text: "this identifier has not been declared",
134+
Span: Span{Start: 25, End: 28},
135+
Text: "this identifier has not been declared",
138136
},
139137
},
140-
Text: `error[E009]: unknown identifier `+"`foo`"+`
138+
Text: `error[E009]: unknown identifier ` + "`foo`" + `
141139
--> test.yar:1:26
142140
|
143141
1 | rule test_2 { condition: foo }
@@ -147,7 +145,6 @@ func TestErrors(t *testing.T) {
147145
}, c.Errors())
148146
}
149147

150-
151148
func TestWarnings(t *testing.T) {
152149
c, err := NewCompiler()
153150
assert.NoError(t, err)
@@ -156,40 +153,40 @@ func TestWarnings(t *testing.T) {
156153

157154
assert.Equal(t, []Warning{
158155
{
159-
Code: "consecutive_jumps",
156+
Code: "consecutive_jumps",
160157
Title: "consecutive jumps in hex pattern `$a`",
161158
Labels: []Label{
162159
{
163-
Level: "warning",
160+
Level: "warning",
164161
CodeOrigin: "",
165-
Span: Span { Start: 30, End: 40 },
166-
Text: "these consecutive jumps will be treated as [0-2]",
162+
Span: Span{Start: 30, End: 40},
163+
Text: "these consecutive jumps will be treated as [0-2]",
167164
},
168165
},
169-
Text: `warning[consecutive_jumps]: consecutive jumps in hex pattern `+"`$a`"+`
166+
Text: `warning[consecutive_jumps]: consecutive jumps in hex pattern ` + "`$a`" + `
170167
--> line:1:31
171168
|
172169
1 | rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a }
173170
| ---------- these consecutive jumps will be treated as [0-2]
174171
|`,
175172
},
176-
{
177-
Code: "slow_pattern",
178-
Title: "slow pattern",
179-
Labels: []Label{
180-
{
181-
Level: "warning",
182-
CodeOrigin: "",
183-
Span: Span { Start: 21, End: 43 },
184-
Text: "this pattern may slow down the scan",
185-
},
186-
},
187-
Text: `warning[slow_pattern]: slow pattern
173+
{
174+
Code: "slow_pattern",
175+
Title: "slow pattern",
176+
Labels: []Label{
177+
{
178+
Level: "warning",
179+
CodeOrigin: "",
180+
Span: Span{Start: 21, End: 43},
181+
Text: "this pattern may slow down the scan",
182+
},
183+
},
184+
Text: `warning[slow_pattern]: slow pattern
188185
--> line:1:22
189186
|
190187
1 | rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a }
191188
| ---------------------- this pattern may slow down the scan
192189
|`,
193-
},
190+
},
194191
}, c.Warnings())
195-
}
192+
}

go/scanner.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ type Scanner struct {
4545
matchingRules []*Rule
4646
}
4747

48-
4948
// ScanResults contains the results of a call to [Scanner.Scan] or [Rules.Scan].
50-
type ScanResults struct{
49+
type ScanResults struct {
5150
matchingRules []*Rule
5251
}
5352

@@ -223,7 +222,7 @@ func (s *Scanner) Scan(buf []byte) (*ScanResults, error) {
223222
err = errors.New(C.GoString(C.yrx_last_error()))
224223
}
225224

226-
scanResults := &ScanResults{ s.matchingRules }
225+
scanResults := &ScanResults{s.matchingRules}
227226
s.matchingRules = nil
228227

229228
return scanResults, err

go/scanner_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestScanner3(t *testing.T) {
5151

5252
s.SetGlobal("var_bool", false)
5353
scanResults, _ = s.Scan([]byte{})
54-
assert.Len(t, scanResults.MatchingRules(), 0)
54+
assert.Len(t, scanResults.MatchingRules(), 0)
5555
}
5656

5757
func TestScanner4(t *testing.T) {
@@ -109,5 +109,5 @@ func TestScannerMetadata(t *testing.T) {
109109
assert.Equal(t, "some_string", matchingRules[0].Metadata()[3].Identifier)
110110
assert.Equal(t, "hello", matchingRules[0].Metadata()[3].Value)
111111
assert.Equal(t, "some_bytes", matchingRules[0].Metadata()[4].Identifier)
112-
assert.Equal(t, []byte{0, 1, 2}, matchingRules[0].Metadata()[4].Value)
112+
assert.Equal(t, []byte{0, 1, 2}, matchingRules[0].Metadata()[4].Value)
113113
}

0 commit comments

Comments
 (0)