pcmt/ent/hibp_create.go
leo e58c15f1c9
All checks were successful
continuous-integration/drone/push Build is passing
ent: add HIBP model
2023-04-21 13:57:32 +02:00

453 lines
13 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"git.dotya.ml/mirre-mt/pcmt/ent/hibp"
"github.com/google/uuid"
)
// HIBPCreate is the builder for creating a HIBP entity.
type HIBPCreate struct {
config
mutation *HIBPMutation
hooks []Hook
}
// SetName sets the "name" field.
func (hc *HIBPCreate) SetName(s string) *HIBPCreate {
hc.mutation.SetName(s)
return hc
}
// SetDomain sets the "domain" field.
func (hc *HIBPCreate) SetDomain(s string) *HIBPCreate {
hc.mutation.SetDomain(s)
return hc
}
// SetBreachDate sets the "breach_date" field.
func (hc *HIBPCreate) SetBreachDate(t time.Time) *HIBPCreate {
hc.mutation.SetBreachDate(t)
return hc
}
// SetAddedDate sets the "added_date" field.
func (hc *HIBPCreate) SetAddedDate(t time.Time) *HIBPCreate {
hc.mutation.SetAddedDate(t)
return hc
}
// SetModifiedDate sets the "modified_date" field.
func (hc *HIBPCreate) SetModifiedDate(t time.Time) *HIBPCreate {
hc.mutation.SetModifiedDate(t)
return hc
}
// SetPwnCount sets the "pwn_count" field.
func (hc *HIBPCreate) SetPwnCount(i int) *HIBPCreate {
hc.mutation.SetPwnCount(i)
return hc
}
// SetDescription sets the "description" field.
func (hc *HIBPCreate) SetDescription(s string) *HIBPCreate {
hc.mutation.SetDescription(s)
return hc
}
// SetDataclasses sets the "dataclasses" field.
func (hc *HIBPCreate) SetDataclasses(s []string) *HIBPCreate {
hc.mutation.SetDataclasses(s)
return hc
}
// SetIsVerified sets the "is_verified" field.
func (hc *HIBPCreate) SetIsVerified(b bool) *HIBPCreate {
hc.mutation.SetIsVerified(b)
return hc
}
// SetNillableIsVerified sets the "is_verified" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsVerified(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsVerified(*b)
}
return hc
}
// SetIsFabricated sets the "is_fabricated" field.
func (hc *HIBPCreate) SetIsFabricated(b bool) *HIBPCreate {
hc.mutation.SetIsFabricated(b)
return hc
}
// SetNillableIsFabricated sets the "is_fabricated" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsFabricated(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsFabricated(*b)
}
return hc
}
// SetIsSensitive sets the "is_sensitive" field.
func (hc *HIBPCreate) SetIsSensitive(b bool) *HIBPCreate {
hc.mutation.SetIsSensitive(b)
return hc
}
// SetNillableIsSensitive sets the "is_sensitive" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsSensitive(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsSensitive(*b)
}
return hc
}
// SetIsRetired sets the "is_retired" field.
func (hc *HIBPCreate) SetIsRetired(b bool) *HIBPCreate {
hc.mutation.SetIsRetired(b)
return hc
}
// SetNillableIsRetired sets the "is_retired" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsRetired(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsRetired(*b)
}
return hc
}
// SetIsSpamList sets the "is_spamList" field.
func (hc *HIBPCreate) SetIsSpamList(b bool) *HIBPCreate {
hc.mutation.SetIsSpamList(b)
return hc
}
// SetNillableIsSpamList sets the "is_spamList" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsSpamList(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsSpamList(*b)
}
return hc
}
// SetIsMalware sets the "is_malware" field.
func (hc *HIBPCreate) SetIsMalware(b bool) *HIBPCreate {
hc.mutation.SetIsMalware(b)
return hc
}
// SetNillableIsMalware sets the "is_malware" field if the given value is not nil.
func (hc *HIBPCreate) SetNillableIsMalware(b *bool) *HIBPCreate {
if b != nil {
hc.SetIsMalware(*b)
}
return hc
}
// SetLogo sets the "logo" field.
func (hc *HIBPCreate) SetLogo(s string) *HIBPCreate {
hc.mutation.SetLogo(s)
return hc
}
// SetID sets the "id" field.
func (hc *HIBPCreate) SetID(u uuid.UUID) *HIBPCreate {
hc.mutation.SetID(u)
return hc
}
// Mutation returns the HIBPMutation object of the builder.
func (hc *HIBPCreate) Mutation() *HIBPMutation {
return hc.mutation
}
// Save creates the HIBP in the database.
func (hc *HIBPCreate) Save(ctx context.Context) (*HIBP, error) {
hc.defaults()
return withHooks[*HIBP, HIBPMutation](ctx, hc.sqlSave, hc.mutation, hc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (hc *HIBPCreate) SaveX(ctx context.Context) *HIBP {
v, err := hc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (hc *HIBPCreate) Exec(ctx context.Context) error {
_, err := hc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (hc *HIBPCreate) ExecX(ctx context.Context) {
if err := hc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (hc *HIBPCreate) defaults() {
if _, ok := hc.mutation.IsVerified(); !ok {
v := hibp.DefaultIsVerified
hc.mutation.SetIsVerified(v)
}
if _, ok := hc.mutation.IsFabricated(); !ok {
v := hibp.DefaultIsFabricated
hc.mutation.SetIsFabricated(v)
}
if _, ok := hc.mutation.IsSensitive(); !ok {
v := hibp.DefaultIsSensitive
hc.mutation.SetIsSensitive(v)
}
if _, ok := hc.mutation.IsRetired(); !ok {
v := hibp.DefaultIsRetired
hc.mutation.SetIsRetired(v)
}
if _, ok := hc.mutation.IsSpamList(); !ok {
v := hibp.DefaultIsSpamList
hc.mutation.SetIsSpamList(v)
}
if _, ok := hc.mutation.IsMalware(); !ok {
v := hibp.DefaultIsMalware
hc.mutation.SetIsMalware(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (hc *HIBPCreate) check() error {
if _, ok := hc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "HIBP.name"`)}
}
if _, ok := hc.mutation.Domain(); !ok {
return &ValidationError{Name: "domain", err: errors.New(`ent: missing required field "HIBP.domain"`)}
}
if _, ok := hc.mutation.BreachDate(); !ok {
return &ValidationError{Name: "breach_date", err: errors.New(`ent: missing required field "HIBP.breach_date"`)}
}
if _, ok := hc.mutation.AddedDate(); !ok {
return &ValidationError{Name: "added_date", err: errors.New(`ent: missing required field "HIBP.added_date"`)}
}
if _, ok := hc.mutation.ModifiedDate(); !ok {
return &ValidationError{Name: "modified_date", err: errors.New(`ent: missing required field "HIBP.modified_date"`)}
}
if _, ok := hc.mutation.PwnCount(); !ok {
return &ValidationError{Name: "pwn_count", err: errors.New(`ent: missing required field "HIBP.pwn_count"`)}
}
if _, ok := hc.mutation.Description(); !ok {
return &ValidationError{Name: "description", err: errors.New(`ent: missing required field "HIBP.description"`)}
}
if _, ok := hc.mutation.Dataclasses(); !ok {
return &ValidationError{Name: "dataclasses", err: errors.New(`ent: missing required field "HIBP.dataclasses"`)}
}
if _, ok := hc.mutation.IsVerified(); !ok {
return &ValidationError{Name: "is_verified", err: errors.New(`ent: missing required field "HIBP.is_verified"`)}
}
if _, ok := hc.mutation.IsFabricated(); !ok {
return &ValidationError{Name: "is_fabricated", err: errors.New(`ent: missing required field "HIBP.is_fabricated"`)}
}
if _, ok := hc.mutation.IsSensitive(); !ok {
return &ValidationError{Name: "is_sensitive", err: errors.New(`ent: missing required field "HIBP.is_sensitive"`)}
}
if _, ok := hc.mutation.IsRetired(); !ok {
return &ValidationError{Name: "is_retired", err: errors.New(`ent: missing required field "HIBP.is_retired"`)}
}
if _, ok := hc.mutation.IsSpamList(); !ok {
return &ValidationError{Name: "is_spamList", err: errors.New(`ent: missing required field "HIBP.is_spamList"`)}
}
if _, ok := hc.mutation.IsMalware(); !ok {
return &ValidationError{Name: "is_malware", err: errors.New(`ent: missing required field "HIBP.is_malware"`)}
}
if _, ok := hc.mutation.Logo(); !ok {
return &ValidationError{Name: "logo", err: errors.New(`ent: missing required field "HIBP.logo"`)}
}
return nil
}
func (hc *HIBPCreate) sqlSave(ctx context.Context) (*HIBP, error) {
if err := hc.check(); err != nil {
return nil, err
}
_node, _spec := hc.createSpec()
if err := sqlgraph.CreateNode(ctx, hc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
hc.mutation.id = &_node.ID
hc.mutation.done = true
return _node, nil
}
func (hc *HIBPCreate) createSpec() (*HIBP, *sqlgraph.CreateSpec) {
var (
_node = &HIBP{config: hc.config}
_spec = sqlgraph.NewCreateSpec(hibp.Table, sqlgraph.NewFieldSpec(hibp.FieldID, field.TypeUUID))
)
if id, ok := hc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := hc.mutation.Name(); ok {
_spec.SetField(hibp.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := hc.mutation.Domain(); ok {
_spec.SetField(hibp.FieldDomain, field.TypeString, value)
_node.Domain = value
}
if value, ok := hc.mutation.BreachDate(); ok {
_spec.SetField(hibp.FieldBreachDate, field.TypeTime, value)
_node.BreachDate = value
}
if value, ok := hc.mutation.AddedDate(); ok {
_spec.SetField(hibp.FieldAddedDate, field.TypeTime, value)
_node.AddedDate = value
}
if value, ok := hc.mutation.ModifiedDate(); ok {
_spec.SetField(hibp.FieldModifiedDate, field.TypeTime, value)
_node.ModifiedDate = value
}
if value, ok := hc.mutation.PwnCount(); ok {
_spec.SetField(hibp.FieldPwnCount, field.TypeInt, value)
_node.PwnCount = value
}
if value, ok := hc.mutation.Description(); ok {
_spec.SetField(hibp.FieldDescription, field.TypeString, value)
_node.Description = value
}
if value, ok := hc.mutation.Dataclasses(); ok {
_spec.SetField(hibp.FieldDataclasses, field.TypeJSON, value)
_node.Dataclasses = value
}
if value, ok := hc.mutation.IsVerified(); ok {
_spec.SetField(hibp.FieldIsVerified, field.TypeBool, value)
_node.IsVerified = value
}
if value, ok := hc.mutation.IsFabricated(); ok {
_spec.SetField(hibp.FieldIsFabricated, field.TypeBool, value)
_node.IsFabricated = value
}
if value, ok := hc.mutation.IsSensitive(); ok {
_spec.SetField(hibp.FieldIsSensitive, field.TypeBool, value)
_node.IsSensitive = value
}
if value, ok := hc.mutation.IsRetired(); ok {
_spec.SetField(hibp.FieldIsRetired, field.TypeBool, value)
_node.IsRetired = value
}
if value, ok := hc.mutation.IsSpamList(); ok {
_spec.SetField(hibp.FieldIsSpamList, field.TypeBool, value)
_node.IsSpamList = value
}
if value, ok := hc.mutation.IsMalware(); ok {
_spec.SetField(hibp.FieldIsMalware, field.TypeBool, value)
_node.IsMalware = value
}
if value, ok := hc.mutation.Logo(); ok {
_spec.SetField(hibp.FieldLogo, field.TypeString, value)
_node.Logo = value
}
return _node, _spec
}
// HIBPCreateBulk is the builder for creating many HIBP entities in bulk.
type HIBPCreateBulk struct {
config
builders []*HIBPCreate
}
// Save creates the HIBP entities in the database.
func (hcb *HIBPCreateBulk) Save(ctx context.Context) ([]*HIBP, error) {
specs := make([]*sqlgraph.CreateSpec, len(hcb.builders))
nodes := make([]*HIBP, len(hcb.builders))
mutators := make([]Mutator, len(hcb.builders))
for i := range hcb.builders {
func(i int, root context.Context) {
builder := hcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*HIBPMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
nodes[i], specs[i] = builder.createSpec()
var err error
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, hcb.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, hcb.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].ID
mutation.done = true
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {
mut = builder.hooks[i](mut)
}
mutators[i] = mut
}(i, ctx)
}
if len(mutators) > 0 {
if _, err := mutators[0].Mutate(ctx, hcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (hcb *HIBPCreateBulk) SaveX(ctx context.Context) []*HIBP {
v, err := hcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (hcb *HIBPCreateBulk) Exec(ctx context.Context) error {
_, err := hcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (hcb *HIBPCreateBulk) ExecX(ctx context.Context) {
if err := hcb.Exec(ctx); err != nil {
panic(err)
}
}