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

     1  package formationtemplate
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/uid"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/repo"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/internal/model"
    11  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // NewConverter creates a new instance of gqlConverter
    16  func NewConverter(webhook WebhookConverter) *converter {
    17  	return &converter{webhook: webhook}
    18  }
    19  
    20  type converter struct {
    21  	webhook WebhookConverter
    22  }
    23  
    24  // FromInputGraphQL converts from GraphQL input to internal model input
    25  func (c *converter) FromInputGraphQL(in *graphql.FormationTemplateInput) (*model.FormationTemplateInput, error) {
    26  	if in == nil {
    27  		return nil, nil
    28  	}
    29  
    30  	webhooks, err := c.webhook.MultipleInputFromGraphQL(in.Webhooks)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	var runtimeTypeDisplayName *string
    36  	if in.RuntimeTypeDisplayName != nil {
    37  		runtimeTypeDisplayName = in.RuntimeTypeDisplayName
    38  	}
    39  
    40  	var runtimeArtifactKind *model.RuntimeArtifactKind
    41  	if in.RuntimeArtifactKind != nil {
    42  		kind := model.RuntimeArtifactKind(*in.RuntimeArtifactKind)
    43  		runtimeArtifactKind = &kind
    44  	}
    45  
    46  	return &model.FormationTemplateInput{
    47  		Name:                   in.Name,
    48  		ApplicationTypes:       in.ApplicationTypes,
    49  		RuntimeTypes:           in.RuntimeTypes,
    50  		RuntimeTypeDisplayName: runtimeTypeDisplayName,
    51  		RuntimeArtifactKind:    runtimeArtifactKind,
    52  		Webhooks:               webhooks,
    53  		LeadingProductIDs:      in.LeadingProductIDs,
    54  	}, nil
    55  }
    56  
    57  // FromModelInputToModel converts from internal model input and id to internal model
    58  func (c *converter) FromModelInputToModel(in *model.FormationTemplateInput, id, tenantID string) *model.FormationTemplate {
    59  	if in == nil {
    60  		return nil
    61  	}
    62  
    63  	uidService := uid.NewService()
    64  	webhooks := make([]*model.Webhook, 0)
    65  	for _, webhookInput := range in.Webhooks {
    66  		webhook := webhookInput.ToWebhook(uidService.Generate(), id, model.FormationTemplateWebhookReference)
    67  		webhooks = append(webhooks, webhook)
    68  	}
    69  
    70  	var tntID *string
    71  	if tenantID != "" {
    72  		tntID = &tenantID
    73  	}
    74  
    75  	return &model.FormationTemplate{
    76  		ID:                     id,
    77  		Name:                   in.Name,
    78  		ApplicationTypes:       in.ApplicationTypes,
    79  		RuntimeTypes:           in.RuntimeTypes,
    80  		RuntimeTypeDisplayName: in.RuntimeTypeDisplayName,
    81  		RuntimeArtifactKind:    in.RuntimeArtifactKind,
    82  		TenantID:               tntID,
    83  		Webhooks:               webhooks,
    84  		LeadingProductIDs:      in.LeadingProductIDs,
    85  	}
    86  }
    87  
    88  // ToGraphQL converts from internal model to GraphQL output
    89  func (c *converter) ToGraphQL(in *model.FormationTemplate) (*graphql.FormationTemplate, error) {
    90  	if in == nil {
    91  		return nil, nil
    92  	}
    93  
    94  	webhooks, err := c.webhook.MultipleToGraphQL(in.Webhooks)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	var runtimeTypeDisplayName *string
   100  	if in.RuntimeTypeDisplayName != nil {
   101  		runtimeTypeDisplayName = in.RuntimeTypeDisplayName
   102  	}
   103  
   104  	var runtimeArtifactKind *graphql.ArtifactType
   105  	if in.RuntimeArtifactKind != nil {
   106  		kind := graphql.ArtifactType(*in.RuntimeArtifactKind)
   107  		runtimeArtifactKind = &kind
   108  	}
   109  
   110  	return &graphql.FormationTemplate{
   111  		ID:                     in.ID,
   112  		Name:                   in.Name,
   113  		ApplicationTypes:       in.ApplicationTypes,
   114  		RuntimeTypes:           in.RuntimeTypes,
   115  		RuntimeTypeDisplayName: runtimeTypeDisplayName,
   116  		RuntimeArtifactKind:    runtimeArtifactKind,
   117  		Webhooks:               webhooks,
   118  		LeadingProductIDs:      in.LeadingProductIDs,
   119  	}, nil
   120  }
   121  
   122  // MultipleToGraphQL converts multiple internal models to GraphQL models
   123  func (c *converter) MultipleToGraphQL(in []*model.FormationTemplate) ([]*graphql.FormationTemplate, error) {
   124  	if in == nil {
   125  		return nil, nil
   126  	}
   127  	formationTemplates := make([]*graphql.FormationTemplate, 0, len(in))
   128  	for _, r := range in {
   129  		if r == nil {
   130  			continue
   131  		}
   132  
   133  		converted, err := c.ToGraphQL(r)
   134  		if err != nil {
   135  			return nil, err
   136  		}
   137  
   138  		formationTemplates = append(formationTemplates, converted)
   139  	}
   140  
   141  	return formationTemplates, nil
   142  }
   143  
   144  // ToEntity converts from internal model to entity
   145  func (c *converter) ToEntity(in *model.FormationTemplate) (*Entity, error) {
   146  	if in == nil {
   147  		return nil, nil
   148  	}
   149  	marshalledApplicationTypes, err := json.Marshal(in.ApplicationTypes)
   150  	if err != nil {
   151  		return nil, errors.Wrap(err, "while marshalling application types")
   152  	}
   153  	marshalledRuntimeTypes, err := json.Marshal(in.RuntimeTypes)
   154  	if err != nil {
   155  		return nil, errors.Wrap(err, "while marshalling application types")
   156  	}
   157  	marshalledLeadingProductIDs, err := json.Marshal(in.LeadingProductIDs)
   158  	if err != nil {
   159  		return nil, errors.Wrap(err, "while marshalling leading product IDs")
   160  	}
   161  
   162  	runtimeArtifactKind := repo.NewNullableString(nil)
   163  	if in.RuntimeArtifactKind != nil {
   164  		kind := string(*in.RuntimeArtifactKind)
   165  		runtimeArtifactKind = repo.NewNullableString(&kind)
   166  	}
   167  
   168  	return &Entity{
   169  		ID:                     in.ID,
   170  		Name:                   in.Name,
   171  		ApplicationTypes:       string(marshalledApplicationTypes),
   172  		RuntimeTypes:           repo.NewValidNullableString(string(marshalledRuntimeTypes)),
   173  		RuntimeTypeDisplayName: repo.NewNullableString(in.RuntimeTypeDisplayName),
   174  		RuntimeArtifactKind:    runtimeArtifactKind,
   175  		LeadingProductIDs:      repo.NewValidNullableString(string(marshalledLeadingProductIDs)),
   176  		TenantID:               repo.NewNullableString(in.TenantID),
   177  	}, nil
   178  }
   179  
   180  // FromEntity converts from entity to internal model
   181  func (c *converter) FromEntity(in *Entity) (*model.FormationTemplate, error) {
   182  	if in == nil {
   183  		return nil, nil
   184  	}
   185  
   186  	var unmarshalledApplicationTypes []string
   187  	err := json.Unmarshal([]byte(in.ApplicationTypes), &unmarshalledApplicationTypes)
   188  	if err != nil {
   189  		return nil, errors.Wrap(err, "while unmarshalling application types")
   190  	}
   191  
   192  	var unmarshalledRuntimeTypes []string
   193  	runtimeTypes := repo.JSONRawMessageFromNullableString(in.RuntimeTypes)
   194  	if runtimeTypes != nil {
   195  		err = json.Unmarshal(runtimeTypes, &unmarshalledRuntimeTypes)
   196  		if err != nil {
   197  			return nil, errors.Wrap(err, "while unmarshalling runtime types")
   198  		}
   199  	}
   200  
   201  	var unmarshalledLeadingProductIDs []string
   202  	leadingProductIDs := repo.JSONRawMessageFromNullableString(in.LeadingProductIDs)
   203  	if leadingProductIDs != nil {
   204  		err = json.Unmarshal(leadingProductIDs, &unmarshalledLeadingProductIDs)
   205  		if err != nil {
   206  			return nil, errors.Wrap(err, "while unmarshalling leading product IDs")
   207  		}
   208  	}
   209  
   210  	var runtimeArtifactKind *model.RuntimeArtifactKind
   211  	if kindPtr := repo.StringPtrFromNullableString(in.RuntimeArtifactKind); kindPtr != nil {
   212  		kind := model.RuntimeArtifactKind(*kindPtr)
   213  		runtimeArtifactKind = &kind
   214  	}
   215  
   216  	return &model.FormationTemplate{
   217  		ID:                     in.ID,
   218  		Name:                   in.Name,
   219  		ApplicationTypes:       unmarshalledApplicationTypes,
   220  		RuntimeTypes:           unmarshalledRuntimeTypes,
   221  		RuntimeTypeDisplayName: repo.StringPtrFromNullableString(in.RuntimeTypeDisplayName),
   222  		RuntimeArtifactKind:    runtimeArtifactKind,
   223  		LeadingProductIDs:      unmarshalledLeadingProductIDs,
   224  		TenantID:               repo.StringPtrFromNullableString(in.TenantID),
   225  	}, nil
   226  }