Skip to content

Commit

Permalink
nullable strings to always yield utf8 string
Browse files Browse the repository at this point in the history
  • Loading branch information
bitcoin-coder-bob committed Feb 2, 2023
1 parent 6c3da86 commit fa12996
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nullable
import (
"database/sql/driver"
"encoding/json"
"fmt"

"gorm.io/gorm"
"gorm.io/gorm/schema"
Expand All @@ -22,8 +23,10 @@ func NewString(value *string) String {
isValid: false,
}
}
// ensures we always get valid utf8 strings
utf8Safe := fmt.Sprintf("%q", *value)
return String{
realValue: *value,
realValue: utf8Safe,
isValid: true,
}
}
Expand Down
19 changes: 19 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ package nullable_test

import (
"testing"
"unicode/utf8"

"github.com/tee8z/nullable"
"gorm.io/gorm/utils/tests"
)

func TestNonUTF8SafeString(t *testing.T) {
// use a regular utf8 valid string
utf8String := "meow i am a cat"
isValid := utf8.Valid([]byte(utf8String))
tests.AssertEqual(t, isValid, true)
nullableString := nullable.NewString(&utf8String)
isValid = utf8.Valid([]byte(*nullableString.Get()))
tests.AssertEqual(t, isValid, true)

// pass in a non utf8 valid string, ensure we get out a valid utf8 string
notUtf8string := "\x17\x87\x1c\x97\xbbs\x159X\xad\xb0[\xca\xe0O\x0b2\xad\xe5\xa3lp\x9b.\xfe\x8eݘ\x15\xfe\xb5Q"
isValid = utf8.Valid([]byte(notUtf8string))
tests.AssertEqual(t, isValid, false)
nullableString = nullable.NewString(&notUtf8string)
isValid = utf8.Valid([]byte(*nullableString.Get()))
tests.AssertEqual(t, isValid, true)
}

func TestScanString(t *testing.T) {
nullableString := nullable.NewString(nil)

Expand Down

0 comments on commit fa12996

Please sign in to comment.