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

     1  package formationconstraint
     2  
     3  import (
     4  	"github.com/kyma-incubator/compass/components/director/internal/model"
     5  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     6  )
     7  
     8  // NewConverter creates a new formation constraint converter
     9  func NewConverter() *converter {
    10  	return &converter{}
    11  }
    12  
    13  type converter struct{}
    14  
    15  // ToGraphQL converts from internal model to GraphQL output
    16  func (c *converter) ToGraphQL(in *model.FormationConstraint) *graphql.FormationConstraint {
    17  	if in == nil {
    18  		return nil
    19  	}
    20  
    21  	return &graphql.FormationConstraint{
    22  		ID:              in.ID,
    23  		Name:            in.Name,
    24  		ConstraintType:  string(in.ConstraintType),
    25  		TargetOperation: string(in.TargetOperation),
    26  		Operator:        in.Operator,
    27  		ResourceType:    string(in.ResourceType),
    28  		ResourceSubtype: in.ResourceSubtype,
    29  		InputTemplate:   in.InputTemplate,
    30  		ConstraintScope: string(in.ConstraintScope),
    31  	}
    32  }
    33  
    34  // MultipleToGraphQL converts multiple internal models to GraphQL models
    35  func (c *converter) MultipleToGraphQL(in []*model.FormationConstraint) []*graphql.FormationConstraint {
    36  	if in == nil {
    37  		return nil
    38  	}
    39  	formationConstraints := make([]*graphql.FormationConstraint, 0, len(in))
    40  	for _, r := range in {
    41  		if r == nil {
    42  			continue
    43  		}
    44  
    45  		fc := c.ToGraphQL(r)
    46  		formationConstraints = append(formationConstraints, fc)
    47  	}
    48  
    49  	return formationConstraints
    50  }
    51  
    52  // ToEntity converts from internal model to entity
    53  func (c *converter) ToEntity(in *model.FormationConstraint) *Entity {
    54  	if in == nil {
    55  		return nil
    56  	}
    57  
    58  	return &Entity{
    59  		ID:              in.ID,
    60  		Name:            in.Name,
    61  		ConstraintType:  string(in.ConstraintType),
    62  		TargetOperation: string(in.TargetOperation),
    63  		Operator:        in.Operator,
    64  		ResourceType:    string(in.ResourceType),
    65  		ResourceSubtype: in.ResourceSubtype,
    66  		InputTemplate:   in.InputTemplate,
    67  		ConstraintScope: string(in.ConstraintScope),
    68  	}
    69  }
    70  
    71  // FromEntity converts from entity to internal model
    72  func (c *converter) FromEntity(e *Entity) *model.FormationConstraint {
    73  	if e == nil {
    74  		return nil
    75  	}
    76  
    77  	return &model.FormationConstraint{
    78  		ID:              e.ID,
    79  		Name:            e.Name,
    80  		ConstraintType:  model.FormationConstraintType(e.ConstraintType),
    81  		TargetOperation: model.TargetOperation(e.TargetOperation),
    82  		Operator:        e.Operator,
    83  		ResourceType:    model.ResourceType(e.ResourceType),
    84  		ResourceSubtype: e.ResourceSubtype,
    85  		InputTemplate:   e.InputTemplate,
    86  		ConstraintScope: model.FormationConstraintScope(e.ConstraintScope),
    87  	}
    88  }
    89  
    90  // FromInputGraphQL converts from GraphQL input to internal model input
    91  func (c *converter) FromInputGraphQL(in *graphql.FormationConstraintInput) *model.FormationConstraintInput {
    92  	return &model.FormationConstraintInput{
    93  		Name:            in.Name,
    94  		ConstraintType:  model.FormationConstraintType(in.ConstraintType),
    95  		TargetOperation: model.TargetOperation(in.TargetOperation),
    96  		Operator:        in.Operator,
    97  		ResourceType:    model.ResourceType(in.ResourceType),
    98  		ResourceSubtype: in.ResourceSubtype,
    99  		InputTemplate:   in.InputTemplate,
   100  		ConstraintScope: model.FormationConstraintScope(in.ConstraintScope),
   101  	}
   102  }
   103  
   104  // FromModelInputToModel converts from internal model input to internal model
   105  func (c *converter) FromModelInputToModel(in *model.FormationConstraintInput, id string) *model.FormationConstraint {
   106  	return &model.FormationConstraint{
   107  		ID:              id,
   108  		Name:            in.Name,
   109  		ConstraintType:  in.ConstraintType,
   110  		TargetOperation: in.TargetOperation,
   111  		Operator:        in.Operator,
   112  		ResourceType:    in.ResourceType,
   113  		ResourceSubtype: in.ResourceSubtype,
   114  		InputTemplate:   in.InputTemplate,
   115  		ConstraintScope: in.ConstraintScope,
   116  	}
   117  }