Closed acheong08 closed 9 months ago
Sidenote:
This can be simplified to just the map.
diff --git a/tokenizer/attributes.go b/tokenizer/attributes.go
index 2d3f4cd..9d1f1eb 100644
--- a/tokenizer/attributes.go
+++ b/tokenizer/attributes.go
@@ -2,7 +2,6 @@ package tokenizer
// Attributes - is a map with support for deterministic order of enumeration
type Attributes struct {
- Keys []string
Map map[string]string
}
@@ -12,7 +11,7 @@ func (a *Attributes) Size() int {
if a == nil {
return 0
}
- return len(a.Keys)
+ return len(a.Map)
}
func (a *Attributes) Append(key, value string) *Attributes {
@@ -25,9 +24,6 @@ func (a *Attributes) Append(key, value string) *Attributes {
}
func (a *Attributes) Set(key, value string) *Attributes {
- if _, ok := a.Map[key]; !ok {
- a.Keys = append(a.Keys, key)
- }
if a.Map == nil {
a.Map = make(map[string]string)
}
@@ -52,8 +48,8 @@ func (a *Attributes) Get(key string) string {
func (a *Attributes) MergeWith(other *Attributes) *Attributes {
if other != nil {
- for _, key := range other.Keys {
- a.Set(key, other.Get(key))
+ for key, value := range other.Map {
+ a.Set(key, value)
}
}
return a
@@ -64,8 +60,8 @@ func (a *Attributes) Entries() []AttributeEntry {
return nil
}
entries := make([]AttributeEntry, 0, len(a.Map))
- for _, key := range a.Keys {
- entries = append(entries, AttributeEntry{Key: key, Value: a.Get(key)})
+ for key, value := range a.Map {
+ entries = append(entries, AttributeEntry{Key: key, Value: value})
}
return entries
}
@@ -75,8 +71,8 @@ func (a *Attributes) GoMap() map[string]string {
return nil
}
entries := make(map[string]string)
- for _, key := range a.Keys {
- entries[key] = a.Get(key)
+ for key, value := range a.Map {
+ entries[key] = value
}
return entries
}
@acheong08, yes, sure - I fixed tests in the commit: https://github.com/sivukhin/godjot/commit/4ea464d83d5e95118b525230bc493f2c7c29a763
I wanted attributes to be deterministic - so I need to manually maintain keys order because Go randomize map iteration order (which is good in some cases but it's kind of annoying if we want to have deterministic result - which we do here)
Feel free to reopen or file new issue if there will be still problems with the tests.
Thanks!
I'm getting a lot of failing tests with
go test ./...
.Are these expected?
Many of them are simply because tokenizer.Attributes is instantiated with nil fields while the tests expect just a
nil
while others expect completely different start/ends