github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/label/entity.go (about)

     1  package label
     2  
     3  import (
     4  	"database/sql"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     7  )
     8  
     9  // Entity is a label entity.
    10  type Entity struct {
    11  	ID               string         `db:"id"`
    12  	TenantID         sql.NullString `db:"tenant_id"`
    13  	Key              string         `db:"key"`
    14  	AppID            sql.NullString `db:"app_id"`
    15  	RuntimeID        sql.NullString `db:"runtime_id"`
    16  	RuntimeContextID sql.NullString `db:"runtime_context_id"`
    17  	AppTemplateID    sql.NullString `db:"app_template_id"`
    18  	Value            string         `db:"value"`
    19  	Version          int            `db:"version"`
    20  }
    21  
    22  // GetID returns the ID of the label.
    23  func (e *Entity) GetID() string {
    24  	return e.ID
    25  }
    26  
    27  // GetParent returns the parent type and the parent ID of the entity.
    28  func (e *Entity) GetParent(_ resource.Type) (resource.Type, string) {
    29  	if e.AppID.Valid {
    30  		return resource.Application, e.AppID.String
    31  	} else if e.RuntimeID.Valid {
    32  		return resource.Runtime, e.RuntimeID.String
    33  	} else if e.RuntimeContextID.Valid {
    34  		return resource.RuntimeContext, e.RuntimeContextID.String
    35  	}
    36  	return resource.Tenant, e.TenantID.String
    37  }
    38  
    39  // Collection is a collection of label entities.
    40  type Collection []Entity
    41  
    42  // Len returns the number of entities in the collection.
    43  func (c Collection) Len() int {
    44  	return len(c)
    45  }
    46  
    47  // DecorateWithTenantID decorates the entity with the given tenant ID.
    48  func (e *Entity) DecorateWithTenantID(tenant string) interface{} {
    49  	return struct {
    50  		*Entity
    51  		TenantID string `db:"tenant_id"`
    52  	}{
    53  		Entity:   e,
    54  		TenantID: tenant,
    55  	}
    56  }