Skip to content

Commit 6d042a3

Browse files
author
Michael Gasch
committed
fix: Minor fixes in docs and style
Signed-off-by: Michael Gasch <[email protected]>
1 parent be2f73f commit 6d042a3

24 files changed

+169
-149
lines changed

arrays_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ const bands = `{
7070
}`
7171

7272
func TestArrayCorrectness(t *testing.T) {
73-
7473
// only pattern3 should match
7574
pattern1 := `{"bands": { "members": { "given": [ "Mick" ], "surname": [ "Strummer" ] } } }`
7675
pattern2 := `{"bands": { "members": { "given": [ "Wata" ], "role": [ "drums" ] } } }`

benchmarks_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import (
1414

1515
const oneMeg = 1024 * 1024
1616

17-
var cityLotsLock sync.Mutex
18-
var cityLotsLines [][]byte
19-
var cityLotsLineCount int
17+
var (
18+
cityLotsLock sync.Mutex
19+
cityLotsLines [][]byte
20+
cityLotsLineCount int
21+
)
2022

2123
func getCityLotsLines(t *testing.T) [][]byte {
2224
cityLotsLock.Lock()
@@ -47,7 +49,6 @@ func getCityLotsLines(t *testing.T) [][]byte {
4749
}
4850

4951
func TestCRANLEIGH(t *testing.T) {
50-
5152
jCranleigh := `{ "type": "Feature", "properties": { "MAPBLKLOT": "7222001", "BLKLOT": "7222001", "BLOCK_NUM": "7222", "LOT_NUM": "001", "FROM_ST": "1", "TO_ST": "1", "STREET": "CRANLEIGH", "ST_TYPE": "DR", "ODD_EVEN": "O" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.472773074480756, 37.73439178240811, 0.0 ], [ -122.47278111723567, 37.73451247621523, 0.0 ], [ -122.47242608711845, 37.73452184591072, 0.0 ], [ -122.472418368113281, 37.734401143064396, 0.0 ], [ -122.472773074480756, 37.73439178240811, 0.0 ] ] ] } }`
5253
j108492 := `{ "type": "Feature", "properties": { "MAPBLKLOT": "0011008", "BLKLOT": "0011008", "BLOCK_NUM": "0011", "LOT_NUM": "008", "FROM_ST": "500", "TO_ST": "550", "STREET": "BEACH", "ST_TYPE": "ST", "ODD_EVEN": "E" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -122.418114728237924, 37.807058866808987, 0.0 ], [ -122.418261722815416, 37.807807921694092, 0.0 ], [ -122.417544151208375, 37.807900142836701, 0.0 ], [ -122.417397010603693, 37.807150305505004, 0.0 ], [ -122.418114728237924, 37.807058866808987, 0.0 ] ] ] } }`
5354
m := newCoreMatcher()

core_matcher.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package quamina
22

3-
// coreMatcher represents an automaton that allows matching sequences of name/value field pairs against
3+
// coreMatcher represents an automaton that allows matching sequences of
4+
// name/value field pairs against
45
// patterns, which are combinations of field names and lists of allowed valued field values.
5-
// The field names are called "Paths" because they encode, in a jsonpath-ish style, the pathSegments from the
6+
// The field names are called "Paths" because they encode, in a jsonpath-ish
7+
// style, the pathSegments from the
68
// root of an incoming object to the leaf field.
7-
// Since the order of fields is generally not significant in encoded data objects, the fields are sorted
9+
// Since the order of fields is generally not significant in encoded data
10+
// objects, the fields are sorted
811
// by name before constructing the automaton, and so are the incoming field lists to be matched, allowing
912
// the automaton to work.
1013

@@ -32,9 +35,6 @@ type coreStart struct {
3235
presumedExistFalseMatches *matchSet
3336
}
3437

35-
// X for anything, should eventually be a generic?
36-
type X any
37-
3838
func newCoreMatcher() *coreMatcher {
3939
m := coreMatcher{}
4040
m.updateable.Store(&coreStart{
@@ -44,6 +44,7 @@ func newCoreMatcher() *coreMatcher {
4444
})
4545
return &m
4646
}
47+
4748
func (m *coreMatcher) start() *coreStart {
4849
return m.updateable.Load().(*coreStart)
4950
}
@@ -146,7 +147,6 @@ type proposedTransition struct {
146147
// matchesForSortedFields runs the provided list of name/value pairs against the automaton and returns
147148
// a possibly-empty list of the patterns that match
148149
func (m *coreMatcher) matchesForSortedFields(fields []Field) *matchSet {
149-
150150
failedExistsFalseMatches := newMatchSet()
151151
matches := newMatchSet()
152152

@@ -203,14 +203,16 @@ func (m *coreMatcher) matchesForSortedFields(fields []Field) *matchSet {
203203

204204
// Arrays are invisible in the automaton. That is to say, if an event has
205205
// { "a": [ 1, 2, 3 ] }
206-
// Then the fields will be a/1, a/2, and a/3
207-
// Same for {"a": [[1, 2], 3]} or any other permutation
208-
// So if you have {"a": [ { "b": 1, "c": 2}, {"b": 3, "c": 4}] }
209-
// then a pattern like { "a": { "b": 1, "c": 4 } } would match.
210-
// To prevent that from happening, each ArrayPos contains two numbers; the first identifies the array in
211-
// the event that this name/val occurred in, the second the position in the array. We don't allow
212-
// transitioning between field values that occur in different positions in the same array.
213-
// See the arrays_test unit test for more examples.
206+
// Then the fields will be a/1, a/2, and a/3 Same for {"a": [[1, 2], 3]} or any
207+
// other permutation So if you have
208+
// {"a": [ { "b": 1, "c": 2}, {"b": 3, "c": 4}] }
209+
// then a pattern like
210+
// { "a": { "b": 1, "c": 4 } }
211+
// would match. To prevent that from happening, each ArrayPos contains two
212+
// numbers; the first identifies the array in
213+
// the event that this name/val occurred in, the second the position in the array. We don't allow
214+
// transitioning between field values that occur in different positions in the same array.
215+
// See the arrays_test unit test for more examples.
214216
func noArrayTrailConflict(from []ArrayPos, to []ArrayPos) bool {
215217
for _, fromAPos := range from {
216218
for _, toAPos := range to {

external_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package quamina_test
22

33
import (
4-
"github.com/timbray/quamina"
54
"testing"
5+
6+
"github.com/timbray/quamina"
67
)
78

89
type fakeFlattener struct {
@@ -12,6 +13,7 @@ type fakeFlattener struct {
1213
func (f *fakeFlattener) Flatten(_ []byte, _ quamina.NameTracker) ([]quamina.Field, error) {
1314
return f.r, nil
1415
}
16+
1517
func (f *fakeFlattener) Copy() quamina.Flattener {
1618
return &fakeFlattener{r: f.r}
1719
}

field_matcher.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type fmFields struct {
2525
func (m *fieldMatcher) fields() *fmFields {
2626
return m.updateable.Load().(*fmFields)
2727
}
28+
2829
func (m *fieldMatcher) update(fields *fmFields) {
2930
m.updateable.Store(fields)
3031
}
@@ -58,7 +59,6 @@ func newFieldMatcher() *fieldMatcher {
5859
}
5960

6061
func (m *fieldMatcher) addTransition(field *patternField) []*fieldMatcher {
61-
6262
// we build the new updateable state in freshStart so we can blsat it in atomically once computed
6363
current := m.fields()
6464
freshStart := &fmFields{}
@@ -107,7 +107,6 @@ func (m *fieldMatcher) addTransition(field *patternField) []*fieldMatcher {
107107
// would be if you had the pattern { "a": [ "foo" ] } and another pattern that matched any value with
108108
// a prefix of "f".
109109
func (m *fieldMatcher) transitionOn(field *Field) []*fieldMatcher {
110-
111110
// are there transitions on this field name?
112111
valMatcher, ok := m.fields().transitions[string(field.Path)]
113112
if !ok {

flatten_json.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ func (fj *flattenJSON) reset() {
3737

3838
// JSON literals
3939
var trueBytes = []byte("true")
40-
var falseBytes = []byte("false")
41-
var nullBytes = []byte("null")
40+
41+
var (
42+
falseBytes = []byte("false")
43+
nullBytes = []byte("null")
44+
)
4245

4346
// fjState - this is a finite state machine parser, or rather a collection of smaller FSM parsers. Some of these
4447
// states are used in only one function, others in multiple places
@@ -394,8 +397,8 @@ func (fj *flattenJSON) readNumber() ([]byte, []byte, error) {
394397
case ',', ']', '}', ' ', '\t', '\n', '\r':
395398
fj.eventIndex--
396399
// TODO: Too expensive; make it possible for people to ask for this
397-
//bytes := fj.event[numStart : fj.eventIndex+1]
398-
//c, err := canonicalize(bytes)
400+
// bytes := fj.event[numStart : fj.eventIndex+1]
401+
// c, err := canonicalize(bytes)
399402
var alt []byte
400403
//if err == nil {
401404
// alt = []byte(c)
@@ -457,7 +460,6 @@ func (fj *flattenJSON) readNumber() ([]byte, []byte, error) {
457460
}
458461

459462
func (fj *flattenJSON) readLiteral(literal []byte) ([]byte, error) {
460-
461463
for _, literalCh := range literal {
462464
if literalCh != fj.ch() {
463465
return nil, fj.error("unknown literal")
@@ -496,7 +498,7 @@ func (fj *flattenJSON) readStringValue() ([]byte, error) {
496498
}
497499

498500
func (fj *flattenJSON) readStringValWithEscapes(nameStart int) ([]byte, error) {
499-
//pointing at '"'
501+
// pointing at '"'
500502
val := []byte{'"'}
501503
var err error
502504
from := nameStart + 1
@@ -625,7 +627,6 @@ func (fj *flattenJSON) readTextWithEscapes(from int) ([]byte, int, error) {
625627
// the from is the offset in fj.event. We return the UTF-8 byte slice, the new setting for fj.eventIndex after
626628
// reading the escapes, and an error if the escape syntax is busted.
627629
func (fj *flattenJSON) readHexUTF16(from int) ([]byte, int, error) {
628-
629630
// in the case that there are multiple \uXXXX in a row, we need to read all of them because some of them
630631
// might be surrogate pairs. So, back up to point at the first \
631632
var codepoints []uint16
@@ -701,6 +702,7 @@ func (fj *flattenJSON) storeArrayElementField(path []byte, val []byte) {
701702
copy(f.ArrayTrail, fj.arrayTrail)
702703
fj.fields = append(fj.fields, f)
703704
}
705+
704706
func (fj *flattenJSON) storeObjectMemberField(path []byte, arrayTrail []ArrayPos, val []byte) {
705707
fj.fields = append(fj.fields, Field{Path: path, ArrayTrail: arrayTrail, Val: val})
706708
}
@@ -709,9 +711,11 @@ func (fj *flattenJSON) enterArray() {
709711
fj.arrayCount++
710712
fj.arrayTrail = append(fj.arrayTrail, ArrayPos{fj.arrayCount, 0})
711713
}
714+
712715
func (fj *flattenJSON) leaveArray() {
713716
fj.arrayTrail = fj.arrayTrail[:len(fj.arrayTrail)-1]
714717
}
718+
715719
func (fj *flattenJSON) stepOneArrayElement() {
716720
fj.arrayTrail[len(fj.arrayTrail)-1].Pos++
717721
}

flatten_json_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ func bequal(a []byte, b []byte) bool {
1616
}
1717
return true
1818
}
19+
1920
func TestFJBasic(t *testing.T) {
2021
j := `{ "a": 1, "b": "two", "c": true, "d": null, "e": { "e1": 2, "e2": 3.02e-5}, "f": [33e2, "x", true, false, null], "g": false}`
2122
allYes := fakeMatcher("a", "b", "c", "d", "e", "e1", "e2", "f", "g")
2223

2324
f := newJSONFlattener()
2425
list, err := f.Flatten([]byte(j), allYes)
25-
2626
if err != nil {
2727
t.Error("E: " + err.Error())
2828
}
@@ -56,7 +56,6 @@ func TestFJBasic(t *testing.T) {
5656
}
5757

5858
func TestFJ10Lines(t *testing.T) {
59-
6059
geo := fakeMatcher("type", "geometry")
6160
testTrackerSelection(newJSONFlattener(), geo, "L0", "testdata/cl-sample-0",
6261
[]string{"type", "geometry\ntype"},
@@ -122,6 +121,7 @@ func TestMinimal(t *testing.T) {
122121
t.Error("Name/Val wrong")
123122
}
124123
}
124+
125125
func testTrackerSelection(fj Flattener, tracker NameTracker, label string, filename string, wantedPaths []string, wantedVals []string, t *testing.T) {
126126
event, err := ioutil.ReadFile(filename)
127127
if err != nil {

flattener.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
package quamina
22

3+
// ArrayPos ... TODO
34
type ArrayPos struct {
45
Array int32
56
Pos int32
67
}
8+
9+
// Field ... TODO
710
type Field struct {
811
Path []byte
912
Val []byte
1013
ArrayTrail []ArrayPos
1114
}
1215

13-
// Flattener is provided as an interface in the hope that flatterners for other non-JSON message formats might
14-
// be implemented.
15-
// How it needs to work, by JSON example:
16-
// { "a": 1, "b": "two", "c": true", "d": nil, "e": { "e1": 2, "e2":, 3.02e-5} "f": [33, "x"]} }
16+
// Flattener is provided as an interface in the hope that flatterners for other
17+
// non-JSON message formats might be implemented. How it needs to work, by JSON
18+
// example:
19+
// { "a": 1, "b": "two", "c": true", "d": nil, "e": { "e1": 2, "e2":, 3.02e-5} "f": [33, "x"]} }
1720
// should produce
18-
// "a", "1"
19-
// "b", "\"two\"",
20-
// "c", "true"
21-
// "d", "nil",
22-
// "e\ne1", "2"
23-
// "e\ne2", "3.02e-5"
24-
// "f", "33"
25-
// "f", "\"x\""
21+
// "a", "1"
22+
// "b", "\"two\"",
23+
// "c", "true"
24+
// "d", "nil",
25+
// "e\ne1", "2"
26+
// "e\ne2", "3.02e-5"
27+
// "f", "33"
28+
// "f", "\"x\""
2629
//
27-
// Let's call the first column, eg "d" and "e\ne1", the pathSegments. For each step i the pathSegments, e.g. "d" and "e1", the
28-
// Flattener shold call nameTracker.IsNameUsed(step) and if that comes back negative, not include any paths which
29-
// don't contain that step.
30-
// So in the example above, if nameTracker.IsNameUsed() only came back true for "a" and "f", then the output
31-
// would be
32-
// "a", "1"
33-
// "f", "33"
34-
// "f", "\"x\""
30+
// Let's call the first column, eg "d" and "e\ne1", the pathSegments. For each
31+
// step i the pathSegments, e.g. "d" and "e1", the Flattener shold call
32+
// nameTracker.IsNameUsed(step) and if that comes back negative, not include any
33+
// paths which don't contain that step. So in the example above, if
34+
// nameTracker.IsNameUsed() only came back true for "a" and "f", then the output
35+
// would be
36+
// "a", "1"
37+
// "f", "33"
38+
// "f", "\"x\""
3539
type Flattener interface {
3640
Flatten(event []byte, tracker NameTracker) ([]Field, error)
3741
Copy() Flattener

list_maker_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,4 @@ func TestListMaker(t *testing.T) {
4848
if len(lists) != wanted {
4949
t.Errorf("Got %d STILL wanted %d", len(lists), wanted)
5050
}
51-
5251
}

live_pattern_state.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@ import (
44
"sync"
55
)
66

7-
// LivePatternsState represents the required capabilities for
8-
// maintaining the set of live patterns.
7+
// LivePatternsState represents the required capabilities for maintaining the
8+
// set of live patterns.
99
type LivePatternsState interface {
1010
// Add adds a new pattern or updates an old pattern.
1111
//
12-
// Note that multiple patterns can be associated with the same
13-
// X.
12+
// Note that multiple patterns can be associated with the same X.
1413
Add(x X, pattern string) error
1514

16-
// Delete removes all patterns associated with the given X and
17-
// returns the number of removed patterns.
15+
// Delete removes all patterns associated with the given X and returns the
16+
// number of removed patterns.
1817
Delete(x X) (int, error)
1918

2019
// Iterate calls the given function for every stored pattern.
2120
Iterate(func(x X, pattern string) error) error
2221

23-
// Contains returns true if x is in the live set; false
24-
// otherwise.
22+
// Contains returns true if x is in the live set; false otherwise.
2523
Contains(x X) (bool, error)
2624
}
2725

@@ -32,8 +30,7 @@ type (
3230

3331
var na = nothing{}
3432

35-
// MemState is a LivePatternsState that is just a map (with a
36-
// RWMutex).
33+
// MemState is a LivePatternsState that is just a map (with a RWMutex).
3734
//
3835
// Since the LivePatternsState implementation can be provided to the
3936
// application, we're keeping things simple here initially.

0 commit comments

Comments
 (0)