clent

package
v0.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package clent includes re-usable components for working with the Ent ORM.

Index

Constants

This section is empty.

Variables

View Source
var Field = struct {
	CLID func(string, string) ent.Field
}{

	CLID: func(name, prefix string) ent.Field {
		if len(prefix) != clid.PrefixSize {
			panic("clent: clid prefix not of length: " + strconv.Itoa(clid.PrefixSize))
		}

		return field.String(name).
			GoType(clid.ID{}).
			DefaultFunc(func() clid.ID {
				return clid.New(prefix)
			}).
			Validate(Validator.CLID(prefix)).
			SchemaType(map[string]string{
				dialect.Postgres: fmt.Sprintf("varchar(%d)", clid.StorageSize),
				dialect.MySQL:    fmt.Sprintf("varchar(%d)", clid.StorageSize),
			})
	},
}

Field is a namespace for re-usable fields.

View Source
var Validator = struct {
	CLID func(string) func(string) error
}{

	CLID: func(expPrefix string) func(s string) error {
		return func(s string) error {
			before, after, found := strings.Cut(s, clid.Separator)
			if !found {
				return errors.New("clent: invalid clid, no separator")
			}

			if _, err := ulid.ParseStrict(after); err != nil {
				return fmt.Errorf("clent: invalid ulid: %w", err)
			}

			if before != expPrefix {
				return fmt.Errorf("clent: provided clid prefix '%s' is invalid, expected: '%s'", before, expPrefix)
			}

			return nil
		}
	},
}

Validator name-spaced re-usable (field validators).

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL