-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor NewSet internals into SetBuilder (#156)
* Refactor SetBuilder out of NewSet * Clean up other new...set constructors * Replace most NewSet usage with SetBuilder * Simplify some SetBuilder helpers * Factor out constants to keep golangci-lint happy
- Loading branch information
Showing
28 changed files
with
288 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package rel | ||
|
||
const ( | ||
// Keeping golangci-lint happy. | ||
sTrue = "true" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package rel | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/arr-ai/frozen" | ||
"github.com/go-errors/errors" | ||
) | ||
|
||
// MustNewSet constructs a genericSet from a set of Values, or panics if construction fails. | ||
func MustNewSet(values ...Value) Set { | ||
s, err := NewSet(values...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return s | ||
} | ||
|
||
// NewSet constructs a genericSet from a set of Values. | ||
func NewSet(values ...Value) (Set, error) { | ||
b := NewSetBuilder() | ||
for _, v := range values { | ||
b.Add(v) | ||
} | ||
return b.Finish() | ||
} | ||
|
||
// NewSetFrom constructs a genericSet from interfaces. | ||
func NewSetFrom(intfs ...interface{}) (Set, error) { | ||
b := NewSetBuilder() | ||
for _, intf := range intfs { | ||
value, err := NewValue(intf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
b.Add(value) | ||
} | ||
return b.Finish() | ||
} | ||
|
||
type SetBuilder struct { | ||
buckets map[interface{}][]Value | ||
} | ||
|
||
func NewSetBuilder() SetBuilder { | ||
return SetBuilder{buckets: map[interface{}][]Value{}} | ||
} | ||
|
||
func (b *SetBuilder) Add(v Value) { | ||
t := reflect.TypeOf(v) | ||
b.buckets[t] = append(b.buckets[t], v) | ||
} | ||
|
||
func (b *SetBuilder) Finish() (Set, error) { | ||
switch len(b.buckets) { | ||
case 0: | ||
return None, nil | ||
case 1: | ||
for typ, values := range b.buckets { | ||
switch typ { | ||
case stringCharTupleType: | ||
return asString(values...), nil | ||
case bytesByteTupleType: | ||
if b, is := asBytes(values...); is { | ||
return b, nil | ||
} | ||
return nil, errors.Errorf("unsupported byte array expr") | ||
case arrayItemTupleType: | ||
return asArray(values...), nil | ||
case dictEntryTupleType: | ||
tuples := make([]DictEntryTuple, 0, len(values)) | ||
for _, value := range values { | ||
tuples = append(tuples, value.(DictEntryTuple)) | ||
} | ||
return NewDict(true, tuples...) | ||
} | ||
} | ||
} | ||
sb := frozen.SetBuilder{} | ||
for _, values := range b.buckets { | ||
for _, value := range values { | ||
sb.Add(value) | ||
} | ||
} | ||
return GenericSet{sb.Finish()}, nil | ||
} |
Oops, something went wrong.