Skip to content

Commit 8691b7b

Browse files
committed
style(go): apply gofmt to all files
1 parent b7007aa commit 8691b7b

File tree

4 files changed

+77
-79
lines changed

4 files changed

+77
-79
lines changed

go/compiler.go

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ func RelaxedReSyntax(yes bool) CompileOption {
8585
// ErrorOnSlowPattern is an option for [NewCompiler] and [Compile] that
8686
// tells the compiler to treat slow patterns as errors instead of warnings.
8787
func ErrorOnSlowPattern(yes bool) CompileOption {
88-
return func(c *Compiler) error {
89-
c.errorOnSlowPattern = yes
90-
return nil
91-
}
88+
return func(c *Compiler) error {
89+
c.errorOnSlowPattern = yes
90+
return nil
91+
}
9292
}
9393

9494
// A structure that contains the options passed to [Compiler.AddSource].
@@ -107,64 +107,63 @@ type SourceOption func(opt *sourceOptions) error
107107
// source's origin. This origin appears in error reports, for instance, if
108108
// if origin is "some_file.yar", error reports will look like:
109109
//
110-
// error: syntax error
111-
// --> some_file.yar:4:17
112-
// |
113-
// 4 | ... more details
114-
//
110+
// error: syntax error
111+
// --> some_file.yar:4:17
112+
// |
113+
// 4 | ... more details
115114
//
116115
// Example:
117116
//
118-
// c := NewCompiler()
119-
// c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
117+
// c := NewCompiler()
118+
// c.AddSource("rule some_rule { condition: true }", WithOrigin("some_file.yar"))
120119
func WithOrigin(origin string) SourceOption {
121-
return func(opts *sourceOptions) error {
122-
opts.origin = origin
123-
return nil
124-
}
120+
return func(opts *sourceOptions) error {
121+
opts.origin = origin
122+
return nil
123+
}
125124
}
126125

127126
// CompileError represents each of the errors returned by [Compiler.Errors].
128127
type CompileError struct {
129128
// Error code (e.g: "E001").
130-
Code string
129+
Code string `json:"code"`
131130
// Error title (e.g: "unknown identifier `foo`").
132-
Title string
131+
Title string `json:"title"`
133132
// Each of the labels in the error report.
134-
Labels []Label
133+
Labels []Label `json:"labels"`
135134
// The error's full report, as shown by the command-line tool.
136-
Text string
135+
Text string `json:"text"`
137136
}
138137

139138
// Warning represents each of the warnings returned by [Compiler.Warnings].
140139
type Warning struct {
141140
// Error code (e.g: "slow_pattern").
142-
Code string
141+
Code string `json:"code"`
143142
// Error title (e.g: "slow pattern").
144-
Title string
143+
Title string `json:"title"`
145144
// Each of the labels in the error report.
146-
Labels []Label
145+
Labels []Label `json:"labels"`
147146
// The error's full report, as shown by the command-line tool.
148-
Text string
147+
Text string `json:"text"`
149148
}
150149

151150
// Label represents a label in a [CompileError].
152151
type Label struct {
153152
// Label's level (e.g: "error", "warning", "info", "note", "help").
154-
Level string
153+
Level string `json:"level"`
155154
// Origin of the code where the error occurred.
156-
CodeOrigin string
155+
CodeOrigin string `json:"code_origin"`
157156
// The code span highlighted by this label.
158-
Span Span
157+
Span Span `json:"span"`
159158
// Text associated to the label.
160-
Text string
159+
Text string `json:"text"`
161160
}
162161

163162
// Span represents the starting and ending point of some piece of source
164163
// code.
165164
type Span struct {
166-
Start int
167-
End int
165+
Start int `json:"start"`
166+
End int `json:"end"`
168167
}
169168

170169
// Error returns the error's full report.
@@ -174,16 +173,16 @@ func (c CompileError) Error() string {
174173

175174
// Compiler represent a YARA compiler.
176175
type Compiler struct {
177-
cCompiler *C.YRX_COMPILER
178-
relaxedReSyntax bool
176+
cCompiler *C.YRX_COMPILER
177+
relaxedReSyntax bool
179178
errorOnSlowPattern bool
180179
ignoredModules map[string]bool
181180
vars map[string]interface{}
182181
features []string
183182
}
184183

185184
// NewCompiler creates a new compiler.
186-
func NewCompiler(opts... CompileOption) (*Compiler, error) {
185+
func NewCompiler(opts ...CompileOption) (*Compiler, error) {
187186
c := &Compiler{
188187
ignoredModules: make(map[string]bool),
189188
vars: make(map[string]interface{}),
@@ -250,11 +249,11 @@ func (c *Compiler) initialize() error {
250249
//
251250
// Examples:
252251
//
253-
// c := NewCompiler()
254-
// c.AddSource("rule foo { condition: true }")
255-
// c.AddSource("rule bar { condition: true }")
256-
// c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
257-
func (c *Compiler) AddSource(src string, opts... SourceOption) error {
252+
// c := NewCompiler()
253+
// c.AddSource("rule foo { condition: true }")
254+
// c.AddSource("rule bar { condition: true }")
255+
// c.AddSource("rule baz { condition: true }", WithOrigin("baz.yar"))
256+
func (c *Compiler) AddSource(src string, opts ...SourceOption) error {
258257
options := &sourceOptions{}
259258
for _, opt := range opts {
260259
opt(options)
@@ -264,10 +263,10 @@ func (c *Compiler) AddSource(src string, opts... SourceOption) error {
264263
defer C.free(unsafe.Pointer(cSrc))
265264

266265
var cOrigin *C.char
267-
if options.origin != "" {
268-
cOrigin = C.CString(options.origin)
269-
defer C.free(unsafe.Pointer(cOrigin))
270-
}
266+
if options.origin != "" {
267+
cOrigin = C.CString(options.origin)
268+
defer C.free(unsafe.Pointer(cOrigin))
269+
}
271270

272271
// The call to runtime.LockOSThread() is necessary to make sure that
273272
// yrx_compiler_add_source and yrx_last_error are called from the same OS
@@ -322,16 +321,16 @@ func (c *Compiler) ignoreModule(module string) {
322321
//
323322
// Examples:
324323
//
325-
// c := NewCompiler()
326-
// // Add some rule named "foo" under the default namespace
327-
// c.AddSource("rule foo { condition: true }")
324+
// c := NewCompiler()
325+
// // Add some rule named "foo" under the default namespace
326+
// c.AddSource("rule foo { condition: true }")
328327
//
329-
// // Create a new namespace named "bar"
330-
// c.NewNamespace("bar")
328+
// // Create a new namespace named "bar"
329+
// c.NewNamespace("bar")
331330
//
332-
// // It's ok to add another rule named "foo", as it is in a different
333-
// // namespace than the previous one.
334-
// c.AddSource("rule foo { condition: true }")
331+
// // It's ok to add another rule named "foo", as it is in a different
332+
// // namespace than the previous one.
333+
// c.AddSource("rule foo { condition: true }")
335334
func (c *Compiler) NewNamespace(namespace string) {
336335
cNamespace := C.CString(namespace)
337336
defer C.free(unsafe.Pointer(cNamespace))

go/compiler_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestVariables(t *testing.T) {
106106

107107
func TestError(t *testing.T) {
108108
_, err := Compile("rule test { condition: foo }")
109-
expected := `error[E009]: unknown identifier `+"`foo`"+`
109+
expected := `error[E009]: unknown identifier ` + "`foo`" + `
110110
--> line:1:24
111111
|
112112
1 | rule test { condition: foo }
@@ -148,17 +148,17 @@ func TestErrors(t *testing.T) {
148148
c.AddSource("rule test_2 { condition: foo }", WithOrigin("test.yar"))
149149
assert.Equal(t, []CompileError{
150150
{
151-
Code: "E009",
151+
Code: "E009",
152152
Title: "unknown identifier `foo`",
153153
Labels: []Label{
154154
{
155-
Level: "error",
155+
Level: "error",
156156
CodeOrigin: "",
157-
Span: Span { Start: 25, End: 28 },
158-
Text: "this identifier has not been declared",
157+
Span: Span{Start: 25, End: 28},
158+
Text: "this identifier has not been declared",
159159
},
160160
},
161-
Text: `error[E009]: unknown identifier `+"`foo`"+`
161+
Text: `error[E009]: unknown identifier ` + "`foo`" + `
162162
--> test.yar:1:26
163163
|
164164
1 | rule test_2 { condition: foo }
@@ -176,40 +176,40 @@ func TestWarnings(t *testing.T) {
176176

177177
assert.Equal(t, []Warning{
178178
{
179-
Code: "consecutive_jumps",
179+
Code: "consecutive_jumps",
180180
Title: "consecutive jumps in hex pattern `$a`",
181181
Labels: []Label{
182182
{
183-
Level: "warning",
183+
Level: "warning",
184184
CodeOrigin: "",
185-
Span: Span { Start: 30, End: 40 },
186-
Text: "these consecutive jumps will be treated as [0-2]",
185+
Span: Span{Start: 30, End: 40},
186+
Text: "these consecutive jumps will be treated as [0-2]",
187187
},
188188
},
189-
Text: `warning[consecutive_jumps]: consecutive jumps in hex pattern `+"`$a`"+`
189+
Text: `warning[consecutive_jumps]: consecutive jumps in hex pattern ` + "`$a`" + `
190190
--> line:1:31
191191
|
192192
1 | rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a }
193193
| ---------- these consecutive jumps will be treated as [0-2]
194194
|`,
195195
},
196-
{
197-
Code: "slow_pattern",
198-
Title: "slow pattern",
199-
Labels: []Label{
200-
{
201-
Level: "warning",
202-
CodeOrigin: "",
203-
Span: Span { Start: 21, End: 43 },
204-
Text: "this pattern may slow down the scan",
205-
},
206-
},
207-
Text: `warning[slow_pattern]: slow pattern
196+
{
197+
Code: "slow_pattern",
198+
Title: "slow pattern",
199+
Labels: []Label{
200+
{
201+
Level: "warning",
202+
CodeOrigin: "",
203+
Span: Span{Start: 21, End: 43},
204+
Text: "this pattern may slow down the scan",
205+
},
206+
},
207+
Text: `warning[slow_pattern]: slow pattern
208208
--> line:1:22
209209
|
210210
1 | rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a }
211211
| ---------------------- this pattern may slow down the scan
212212
|`,
213-
},
213+
},
214214
}, c.Warnings())
215-
}
215+
}

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)