mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
schema/mixin: add privacy policy to mixin (#821)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -23,13 +23,13 @@ type Schema struct {
|
||||
Fields []*Field `json:"fields,omitempty"`
|
||||
Indexes []*Index `json:"indexes,omitempty"`
|
||||
Hooks []*Position `json:"hooks,omitempty"`
|
||||
Policy bool `json:"policy,omitempty"`
|
||||
Policy []*Position `json:"policy,omitempty"`
|
||||
}
|
||||
|
||||
// Position describes a field position in the schema.
|
||||
// Position describes a position in the schema.
|
||||
type Position struct {
|
||||
Index int // Field index in the field list.
|
||||
MixedIn bool // Indicates if the field was mixed-in.
|
||||
Index int // Index in the field/hook list.
|
||||
MixedIn bool // Indicates if the schema object was mixed-in.
|
||||
MixinIndex int // Mixin index in the mixin list.
|
||||
}
|
||||
|
||||
@@ -248,6 +248,16 @@ func (s *Schema) loadMixin(schema ent.Interface) error {
|
||||
MixinIndex: i,
|
||||
})
|
||||
}
|
||||
policy, err := safePolicy(mx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mixin %q: %v", name, err)
|
||||
}
|
||||
if policy != nil {
|
||||
s.Policy = append(s.Policy, &Position{
|
||||
MixedIn: true,
|
||||
MixinIndex: i,
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -288,7 +298,9 @@ func (s *Schema) loadPolicy(schema ent.Interface) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Policy = policy != nil
|
||||
if policy != nil {
|
||||
s.Policy = append(s.Policy, &Position{})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -365,7 +377,7 @@ func safeHooks(schema interface{ Hooks() []ent.Hook }) (hooks []ent.Hook, err er
|
||||
}
|
||||
|
||||
// safePolicy wraps the schema.Policy method with recover to ensure no panics in marshaling.
|
||||
func safePolicy(schema ent.Interface) (policy ent.Policy, err error) {
|
||||
func safePolicy(schema interface{ Policy() ent.Policy }) (policy ent.Policy, err error) {
|
||||
defer func() {
|
||||
if v := recover(); v != nil {
|
||||
err = fmt.Errorf("schema.Policy panics: %v", v)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package load
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"testing"
|
||||
@@ -297,6 +298,19 @@ func (HooksMixin) Hooks() []ent.Hook {
|
||||
}
|
||||
}
|
||||
|
||||
type BoringPolicy struct{}
|
||||
|
||||
func (BoringPolicy) EvalMutation(context.Context, ent.Mutation) error { return nil }
|
||||
func (BoringPolicy) EvalQuery(context.Context, ent.Query) error { return nil }
|
||||
|
||||
type PrivacyMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (PrivacyMixin) Policy() ent.Policy {
|
||||
return BoringPolicy{}
|
||||
}
|
||||
|
||||
type WithMixin struct {
|
||||
ent.Schema
|
||||
}
|
||||
@@ -305,6 +319,7 @@ func (WithMixin) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
TimeMixin{},
|
||||
HooksMixin{},
|
||||
PrivacyMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +349,10 @@ func (WithMixin) Hooks() []ent.Hook {
|
||||
}
|
||||
}
|
||||
|
||||
func (WithMixin) Policy() ent.Policy {
|
||||
return BoringPolicy{}
|
||||
}
|
||||
|
||||
func TestMarshalMixin(t *testing.T) {
|
||||
d := WithMixin{}
|
||||
buf, err := MarshalSchema(d)
|
||||
@@ -406,4 +425,10 @@ func TestMarshalMixin(t *testing.T) {
|
||||
require.Equal(t, []string{"owner"}, schema.Indexes[1].Edges)
|
||||
require.True(t, schema.Indexes[1].Unique)
|
||||
})
|
||||
|
||||
t.Run("Policy", func(t *testing.T) {
|
||||
require.Len(t, schema.Policy, 2)
|
||||
require.True(t, schema.Policy[0].MixedIn)
|
||||
require.False(t, schema.Policy[1].MixedIn)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user