// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/buildbarn/bb-portal/ent/gen/ent/memorymetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/metrics"
)

// MemoryMetrics is the model entity for the MemoryMetrics schema.
type MemoryMetrics struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// PeakPostGcHeapSize holds the value of the "peak_post_gc_heap_size" field.
	PeakPostGcHeapSize int64 `json:"peak_post_gc_heap_size,omitempty"`
	// UsedHeapSizePostBuild holds the value of the "used_heap_size_post_build" field.
	UsedHeapSizePostBuild int64 `json:"used_heap_size_post_build,omitempty"`
	// PeakPostGcTenuredSpaceHeapSize holds the value of the "peak_post_gc_tenured_space_heap_size" field.
	PeakPostGcTenuredSpaceHeapSize int64 `json:"peak_post_gc_tenured_space_heap_size,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the MemoryMetricsQuery when eager-loading is set.
	Edges                  MemoryMetricsEdges `json:"edges"`
	metrics_memory_metrics *int
	selectValues           sql.SelectValues
}

// MemoryMetricsEdges holds the relations/edges for other nodes in the graph.
type MemoryMetricsEdges struct {
	// Metrics holds the value of the metrics edge.
	Metrics *Metrics `json:"metrics,omitempty"`
	// GarbageMetrics holds the value of the garbage_metrics edge.
	GarbageMetrics []*GarbageMetrics `json:"garbage_metrics,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]bool
	// totalCount holds the count of the edges above.
	totalCount [2]map[string]int

	namedGarbageMetrics map[string][]*GarbageMetrics
}

// MetricsOrErr returns the Metrics value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e MemoryMetricsEdges) MetricsOrErr() (*Metrics, error) {
	if e.Metrics != nil {
		return e.Metrics, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: metrics.Label}
	}
	return nil, &NotLoadedError{edge: "metrics"}
}

// GarbageMetricsOrErr returns the GarbageMetrics value or an error if the edge
// was not loaded in eager-loading.
func (e MemoryMetricsEdges) GarbageMetricsOrErr() ([]*GarbageMetrics, error) {
	if e.loadedTypes[1] {
		return e.GarbageMetrics, nil
	}
	return nil, &NotLoadedError{edge: "garbage_metrics"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*MemoryMetrics) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case memorymetrics.FieldID, memorymetrics.FieldPeakPostGcHeapSize, memorymetrics.FieldUsedHeapSizePostBuild, memorymetrics.FieldPeakPostGcTenuredSpaceHeapSize:
			values[i] = new(sql.NullInt64)
		case memorymetrics.ForeignKeys[0]: // metrics_memory_metrics
			values[i] = new(sql.NullInt64)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the MemoryMetrics fields.
func (mm *MemoryMetrics) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case memorymetrics.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			mm.ID = int(value.Int64)
		case memorymetrics.FieldPeakPostGcHeapSize:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field peak_post_gc_heap_size", values[i])
			} else if value.Valid {
				mm.PeakPostGcHeapSize = value.Int64
			}
		case memorymetrics.FieldUsedHeapSizePostBuild:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field used_heap_size_post_build", values[i])
			} else if value.Valid {
				mm.UsedHeapSizePostBuild = value.Int64
			}
		case memorymetrics.FieldPeakPostGcTenuredSpaceHeapSize:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field peak_post_gc_tenured_space_heap_size", values[i])
			} else if value.Valid {
				mm.PeakPostGcTenuredSpaceHeapSize = value.Int64
			}
		case memorymetrics.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field metrics_memory_metrics", value)
			} else if value.Valid {
				mm.metrics_memory_metrics = new(int)
				*mm.metrics_memory_metrics = int(value.Int64)
			}
		default:
			mm.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the MemoryMetrics.
// This includes values selected through modifiers, order, etc.
func (mm *MemoryMetrics) Value(name string) (ent.Value, error) {
	return mm.selectValues.Get(name)
}

// QueryMetrics queries the "metrics" edge of the MemoryMetrics entity.
func (mm *MemoryMetrics) QueryMetrics() *MetricsQuery {
	return NewMemoryMetricsClient(mm.config).QueryMetrics(mm)
}

// QueryGarbageMetrics queries the "garbage_metrics" edge of the MemoryMetrics entity.
func (mm *MemoryMetrics) QueryGarbageMetrics() *GarbageMetricsQuery {
	return NewMemoryMetricsClient(mm.config).QueryGarbageMetrics(mm)
}

// Update returns a builder for updating this MemoryMetrics.
// Note that you need to call MemoryMetrics.Unwrap() before calling this method if this MemoryMetrics
// was returned from a transaction, and the transaction was committed or rolled back.
func (mm *MemoryMetrics) Update() *MemoryMetricsUpdateOne {
	return NewMemoryMetricsClient(mm.config).UpdateOne(mm)
}

// Unwrap unwraps the MemoryMetrics entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (mm *MemoryMetrics) Unwrap() *MemoryMetrics {
	_tx, ok := mm.config.driver.(*txDriver)
	if !ok {
		panic("ent: MemoryMetrics is not a transactional entity")
	}
	mm.config.driver = _tx.drv
	return mm
}

// String implements the fmt.Stringer.
func (mm *MemoryMetrics) String() string {
	var builder strings.Builder
	builder.WriteString("MemoryMetrics(")
	builder.WriteString(fmt.Sprintf("id=%v, ", mm.ID))
	builder.WriteString("peak_post_gc_heap_size=")
	builder.WriteString(fmt.Sprintf("%v", mm.PeakPostGcHeapSize))
	builder.WriteString(", ")
	builder.WriteString("used_heap_size_post_build=")
	builder.WriteString(fmt.Sprintf("%v", mm.UsedHeapSizePostBuild))
	builder.WriteString(", ")
	builder.WriteString("peak_post_gc_tenured_space_heap_size=")
	builder.WriteString(fmt.Sprintf("%v", mm.PeakPostGcTenuredSpaceHeapSize))
	builder.WriteByte(')')
	return builder.String()
}

// NamedGarbageMetrics returns the GarbageMetrics named value or an error if the edge was not
// loaded in eager-loading with this name.
func (mm *MemoryMetrics) NamedGarbageMetrics(name string) ([]*GarbageMetrics, error) {
	if mm.Edges.namedGarbageMetrics == nil {
		return nil, &NotLoadedError{edge: name}
	}
	nodes, ok := mm.Edges.namedGarbageMetrics[name]
	if !ok {
		return nil, &NotLoadedError{edge: name}
	}
	return nodes, nil
}

func (mm *MemoryMetrics) appendNamedGarbageMetrics(name string, edges ...*GarbageMetrics) {
	if mm.Edges.namedGarbageMetrics == nil {
		mm.Edges.namedGarbageMetrics = make(map[string][]*GarbageMetrics)
	}
	if len(edges) == 0 {
		mm.Edges.namedGarbageMetrics[name] = []*GarbageMetrics{}
	} else {
		mm.Edges.namedGarbageMetrics[name] = append(mm.Edges.namedGarbageMetrics[name], edges...)
	}
}

// MemoryMetricsSlice is a parsable slice of MemoryMetrics.
type MemoryMetricsSlice []*MemoryMetrics
