File: group.go

package info (click to toggle)
golang-entgo-ent 0.11.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,976 kB
  • sloc: javascript: 641; makefile: 8; sql: 2
file content (65 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package schema

import (
	"regexp"
	"strings"

	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
)

// CheckError is returned by the validators.
type CheckError struct {
	msg string
}

func (c CheckError) Error() string {
	return c.msg
}

// Group holds the schema for the group entity.
type Group struct {
	ent.Schema
}

// Fields of the group.
func (Group) Fields() []ent.Field {
	return []ent.Field{
		field.Bool("active").
			Default(true),
		field.Time("expire"),
		field.String("type").
			Optional().
			Nillable().
			MinLen(3).
			MaxLen(255),
		field.Int("max_users").
			Optional().
			Positive().
			Default(10),
		field.String("name").
			Comment("Name field with multiple validators").
			Match(regexp.MustCompile("[a-zA-Z_]+$")).
			Validate(func(s string) error {
				if strings.ToLower(s) == s {
					return CheckError{msg: "last name must begin with uppercase"}
				}
				return nil
			}),
	}
}

// Edges of the group.
func (Group) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("files", File.Type),
		edge.To("blocked", User.Type),
		edge.From("users", User.Type).Ref("groups"),
		edge.To("info", GroupInfo.Type).Unique().Required(),
	}
}