Skip to content

Commit

Permalink
added check
Browse files Browse the repository at this point in the history
  • Loading branch information
tee8z committed Feb 2, 2023
1 parent fa12996 commit adf2185
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"unicode/utf8"

"gorm.io/gorm"
"gorm.io/gorm/schema"
Expand All @@ -23,10 +24,12 @@ func NewString(value *string) String {
isValid: false,
}
}
// ensures we always get valid utf8 strings
utf8Safe := fmt.Sprintf("%q", *value)
isValid := utf8.Valid([]byte(*value))
if !isValid {
*value = fmt.Sprintf("%q", *value)
}
return String{
realValue: utf8Safe,
realValue: *value,
isValid: true,
}
}
Expand Down Expand Up @@ -88,6 +91,7 @@ func (n String) Value() (driver.Value, error) {
if !n.isValid {
return nil, nil
}

return n.realValue, nil
}

Expand Down

0 comments on commit adf2185

Please sign in to comment.