-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(kv): refactor kv.Entity to provide interface for PK and unique …
…keys
- Loading branch information
1 parent
5560623
commit 7fe5cf9
Showing
9 changed files
with
177 additions
and
176 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
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,49 @@ | ||
package kv | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/influxdata/influxdb" | ||
) | ||
|
||
// EncodeFn returns an encoding when called. Closures are your friend here. | ||
type EncodeFn func() ([]byte, error) | ||
|
||
// Encode concatenates a list of encodings together. | ||
func Encode(encodings ...EncodeFn) EncodeFn { | ||
return func() ([]byte, error) { | ||
var key []byte | ||
for _, enc := range encodings { | ||
part, err := enc() | ||
if err != nil { | ||
return key, err | ||
} | ||
key = append(key, part...) | ||
} | ||
return key, nil | ||
} | ||
} | ||
|
||
// EncString encodes a string. | ||
func EncString(str string) EncodeFn { | ||
return func() ([]byte, error) { | ||
return []byte(str), nil | ||
} | ||
} | ||
|
||
// EncStringCaseInsensitive encodes a string and makes it case insensitive by lower casing | ||
// everything. | ||
func EncStringCaseInsensitive(str string) EncodeFn { | ||
return EncString(strings.ToLower(str)) | ||
} | ||
|
||
// EncID encodes an influx ID. | ||
func EncID(id influxdb.ID) EncodeFn { | ||
return func() ([]byte, error) { | ||
if id == 0 { | ||
return nil, errors.New("no ID was provided") | ||
} | ||
return id.Encode() | ||
} | ||
} |
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
Oops, something went wrong.