add charset support for fields

Summary: Basically, adding support for Hebrew characters.

Reviewed By: alexsn

Differential Revision: D16068537

fbshipit-source-id: 4e934da5ea97c9e804317f746556ab1d51faebcc
This commit is contained in:
Ariel Mashraki
2019-07-01 08:06:04 -07:00
committed by Facebook Github Bot
parent 37ae2b744e
commit e8e96f014f
24 changed files with 417 additions and 182 deletions

View File

@@ -77,6 +77,7 @@ type Field struct {
tag string
name string
comment string
charset string
unique bool
nullable bool
optional bool
@@ -383,6 +384,16 @@ func (b *stringBuilder) StructTag(s string) *stringBuilder {
return b
}
// SetCharset sets the character set attribute for character fields.
// For example, utf8 or utf8mb4 in MySQL.
func (b *stringBuilder) SetCharset(s string) *stringBuilder {
b.charset = s
return b
}
// Charset returns the character set of the field.
func (b stringBuilder) Charset() string { return b.charset }
// timeBuilder is the builder for time fields.
type timeBuilder struct {
Field
@@ -450,3 +461,8 @@ func (b *boolBuilder) StructTag(s string) *boolBuilder {
b.tag = s
return b
}
// Charseter is the interface that wraps the Charset method.
type Charseter interface {
Charset() string
}

View File

@@ -1,11 +1,12 @@
package field_test
import (
"fbc/ent/field"
"github.com/stretchr/testify/require"
"regexp"
"testing"
"fbc/ent"
"fbc/ent/field"
"github.com/stretchr/testify/assert"
)
@@ -53,6 +54,13 @@ func TestString(t *testing.T) {
assert.Len(t, f.Validators(), 2)
}
func TestCharset(t *testing.T) {
var f ent.Field = field.String("name").SetCharset("utf8")
cs, ok := f.(field.Charseter)
assert.True(t, ok, "string field implements the Charseter interface")
assert.Equal(t, "utf8", cs.Charset())
}
func TestTime(t *testing.T) {
f := field.Time("created_at")
assert.Equal(t, "created_at", f.Name())
@@ -62,5 +70,5 @@ func TestTime(t *testing.T) {
func TestField_Tag(t *testing.T) {
f := field.Bool("expired").StructTag(`json:"expired,omitempty"`)
require.Equal(t, `json:"expired,omitempty"`, f.Tag())
assert.Equal(t, `json:"expired,omitempty"`, f.Tag())
}