entc/schema: adding support for immutable fields (#1340)

Summary:
Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1340

Immutable fields are ent fields which cannot be updated

Reviewed By: a8m

Differential Revision: D16899625

fbshipit-source-id: ca482c66570e84ca9a1e0a03f729847561f2816b
This commit is contained in:
Alex Snast
2019-08-20 02:15:55 -07:00
committed by Facebook Github Bot
parent 10b8c52363
commit 85b29631a8
14 changed files with 173 additions and 106 deletions

View File

@@ -117,6 +117,7 @@ type Field struct {
unique bool
nillable bool
optional bool
immutable bool
value interface{}
validators []interface{}
}
@@ -176,6 +177,9 @@ func (f Field) IsNillable() bool { return f.nillable }
// IsOptional returns is this field is an optional field.
func (f Field) IsOptional() bool { return f.optional }
// IsImmutable returns is this field is an immutable field.
func (f Field) IsImmutable() bool { return f.immutable }
// IsUnique returns is this field is a unique field.
func (f Field) IsUnique() bool { return f.unique }
@@ -264,6 +268,12 @@ func (b *intBuilder) Optional() *intBuilder {
return b
}
// Immutable indicates that this field cannot be updated.
func (b *intBuilder) Immutable() *intBuilder {
b.immutable = true
return b
}
// StructTag sets the struct tag of the field.
func (b *intBuilder) StructTag(s string) *intBuilder {
b.tag = s
@@ -355,6 +365,12 @@ func (b *floatBuilder) Optional() *floatBuilder {
return b
}
// Immutable indicates that this field cannot be updated.
func (b *floatBuilder) Immutable() *floatBuilder {
b.immutable = true
return b
}
// StructTag sets the struct tag of the field.
func (b *floatBuilder) StructTag(s string) *floatBuilder {
b.tag = s
@@ -440,6 +456,12 @@ func (b *stringBuilder) Optional() *stringBuilder {
return b
}
// Immutable indicates that this field cannot be updated.
func (b *stringBuilder) Immutable() *stringBuilder {
b.immutable = true
return b
}
// Comment sets the comment of the field.
func (b *stringBuilder) Comment(c string) *stringBuilder {
return b
@@ -484,6 +506,12 @@ func (b *timeBuilder) Optional() *timeBuilder {
return b
}
// Immutable indicates that this field cannot be updated.
func (b *timeBuilder) Immutable() *timeBuilder {
b.immutable = true
return b
}
// Comment sets the comment of the field.
func (b *timeBuilder) Comment(c string) *timeBuilder {
return b
@@ -531,6 +559,12 @@ func (b *boolBuilder) Optional() *boolBuilder {
return b
}
// Immutable indicates that this field cannot be updated.
func (b *boolBuilder) Immutable() *boolBuilder {
b.immutable = true
return b
}
// Comment sets the comment of the field.
func (b *boolBuilder) Comment(c string) *boolBuilder {
return b
@@ -567,6 +601,12 @@ func (b *bytesBuilder) Optional() *bytesBuilder {
return b
}
// Immutable indicates that this field cannot be updated.
func (b *bytesBuilder) Immutable() *bytesBuilder {
b.immutable = true
return b
}
// Comment sets the comment of the field.
func (b *bytesBuilder) Comment(c string) *bytesBuilder {
return b

View File

@@ -25,6 +25,7 @@ func TestInt(t *testing.T) {
f = field.Int("age").Range(20, 40).Nillable()
assert.False(t, f.HasDefault())
assert.True(t, f.IsNillable())
assert.False(t, f.IsImmutable())
assert.Len(t, f.Validators(), 1)
assert.Equal(t, field.TypeInt8, field.Int8("age").Type())
@@ -44,10 +45,11 @@ func TestFloat(t *testing.T) {
}
func TestBool(t *testing.T) {
f := field.Bool("active").Default(true)
f := field.Bool("active").Default(true).Immutable()
assert.Equal(t, "active", f.Name())
assert.Equal(t, field.TypeBool, f.Type())
assert.True(t, f.HasDefault())
assert.True(t, f.IsImmutable())
assert.Equal(t, true, f.Value())
}