Skip to content
Snippets Groups Projects

Initial implementation of an elasticsearch indexer in Go

Merged Nick Thomas requested to merge 1-initial-implementation into master
All threads resolved!
9 files
+ 66
42
Compare changes
  • Side-by-side
  • Inline
Files
9
+ 18
6
package indexer
import (
"github.com/goodsign/icu"
"fmt"
"log"
"github.com/lupine/icu"
)
var (
@@ -20,6 +23,7 @@ func init() {
func tryEncodeString(s string) string {
encoded, err := encodeString(s)
if err != nil {
log.Println(err)
return s // TODO: Run it through the UTF-8 replacement encoder
}
@@ -29,6 +33,7 @@ func tryEncodeString(s string) string {
func tryEncodeBytes(b []byte) string {
encoded, err := encodeBytes(b)
if err != nil {
log.Println(err)
s := string(b)
return s // TODO: Run it through the UTF-8 replacement encoder
}
@@ -42,15 +47,22 @@ func encodeString(s string) (string, error) {
// encodeString converts a string from an arbitrary encoding to UTF-8
func encodeBytes(b []byte) (string, error) {
if len(b) == 0 {
return "", nil
}
matches, err := detector.GuessCharset(b)
if err != nil {
return "", err
return "", fmt.Errorf("Couldn't guess charset: %s", err)
}
utf8, err := converter.ConvertToUtf8(b, matches[0].Charset)
if err != nil {
return "", err
// Try encoding for each match, returning the first that succeeds
for _, match := range matches {
utf8, err := converter.ConvertToUtf8(b, match.Charset)
if err == nil {
return string(utf8), nil
}
}
return string(utf8), nil
return "", fmt.Errorf("Failed to convert from %s to UTF-8", matches[0].Charset)
}
Loading