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

     1  package eventdef
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/str"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/domain/version"
     9  	"github.com/kyma-incubator/compass/components/director/internal/model"
    10  	"github.com/kyma-incubator/compass/components/director/internal/repo"
    11  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // VersionConverter converts Version between model.Version, graphql.Version and repo-layer version.Version
    16  //
    17  //go:generate mockery --name=VersionConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    18  type VersionConverter interface {
    19  	ToGraphQL(in *model.Version) *graphql.Version
    20  	InputFromGraphQL(in *graphql.VersionInput) *model.VersionInput
    21  	FromEntity(version version.Version) *model.Version
    22  	ToEntity(version model.Version) version.Version
    23  }
    24  
    25  // SpecConverter converts Specifications between the model.Spec service-layer representation and the graphql-layer representation graphql.EventSpec.
    26  //
    27  //go:generate mockery --name=SpecConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    28  type SpecConverter interface {
    29  	ToGraphQLEventSpec(in *model.Spec) (*graphql.EventSpec, error)
    30  	InputFromGraphQLEventSpec(in *graphql.EventSpecInput) (*model.SpecInput, error)
    31  }
    32  
    33  type converter struct {
    34  	vc VersionConverter
    35  	sc SpecConverter
    36  }
    37  
    38  // NewConverter returns a new Converter that can later be used to make the conversions between the GraphQL, service, and repository layer representations of a Compass EventDefinition.
    39  func NewConverter(vc VersionConverter, sc SpecConverter) *converter {
    40  	return &converter{vc: vc, sc: sc}
    41  }
    42  
    43  // ToGraphQL converts the provided service-layer representation of an EventDefinition to the graphql-layer one.
    44  func (c *converter) ToGraphQL(in *model.EventDefinition, spec *model.Spec, bundleRef *model.BundleReference) (*graphql.EventDefinition, error) {
    45  	if in == nil {
    46  		return nil, nil
    47  	}
    48  
    49  	s, err := c.sc.ToGraphQLEventSpec(spec)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	var bundleID string
    55  	if bundleRef != nil && bundleRef.BundleID != nil {
    56  		bundleID = *bundleRef.BundleID
    57  	}
    58  
    59  	return &graphql.EventDefinition{
    60  		BundleID:    bundleID,
    61  		Name:        in.Name,
    62  		Description: in.Description,
    63  		Group:       in.Group,
    64  		Spec:        s,
    65  		Version:     c.vc.ToGraphQL(in.Version),
    66  		BaseEntity: &graphql.BaseEntity{
    67  			ID:        in.ID,
    68  			Ready:     in.Ready,
    69  			CreatedAt: timePtrToTimestampPtr(in.CreatedAt),
    70  			UpdatedAt: timePtrToTimestampPtr(in.UpdatedAt),
    71  			DeletedAt: timePtrToTimestampPtr(in.DeletedAt),
    72  			Error:     in.Error,
    73  		},
    74  	}, nil
    75  }
    76  
    77  // MultipleToGraphQL converts the provided service-layer representations of an EventDefinition to the graphql-layer ones.
    78  func (c *converter) MultipleToGraphQL(in []*model.EventDefinition, specs []*model.Spec, bundleRefs []*model.BundleReference) ([]*graphql.EventDefinition, error) {
    79  	if len(in) != len(specs) || len(in) != len(bundleRefs) || len(bundleRefs) != len(specs) {
    80  		return nil, errors.New("different events, specs and bundleRefs count provided")
    81  	}
    82  
    83  	events := make([]*graphql.EventDefinition, 0, len(in))
    84  	for i, e := range in {
    85  		if e == nil {
    86  			continue
    87  		}
    88  
    89  		event, err := c.ToGraphQL(e, specs[i], bundleRefs[i])
    90  		if err != nil {
    91  			return nil, err
    92  		}
    93  
    94  		events = append(events, event)
    95  	}
    96  
    97  	return events, nil
    98  }
    99  
   100  // MultipleInputFromGraphQL converts the provided graphql-layer representations of an EventDefinition to the service-layer ones.
   101  func (c *converter) MultipleInputFromGraphQL(in []*graphql.EventDefinitionInput) ([]*model.EventDefinitionInput, []*model.SpecInput, error) {
   102  	eventDefs := make([]*model.EventDefinitionInput, 0, len(in))
   103  	specs := make([]*model.SpecInput, 0, len(in))
   104  
   105  	for _, item := range in {
   106  		event, spec, err := c.InputFromGraphQL(item)
   107  		if err != nil {
   108  			return nil, nil, err
   109  		}
   110  
   111  		eventDefs = append(eventDefs, event)
   112  		specs = append(specs, spec)
   113  	}
   114  
   115  	return eventDefs, specs, nil
   116  }
   117  
   118  // InputFromGraphQL converts the provided graphql-layer representation of an EventDefinition to the service-layer one.
   119  func (c *converter) InputFromGraphQL(in *graphql.EventDefinitionInput) (*model.EventDefinitionInput, *model.SpecInput, error) {
   120  	if in == nil {
   121  		return nil, nil, nil
   122  	}
   123  
   124  	spec, err := c.sc.InputFromGraphQLEventSpec(in.Spec)
   125  	if err != nil {
   126  		return nil, nil, err
   127  	}
   128  
   129  	return &model.EventDefinitionInput{
   130  		Name:         in.Name,
   131  		Description:  in.Description,
   132  		Group:        in.Group,
   133  		VersionInput: c.vc.InputFromGraphQL(in.Version),
   134  	}, spec, nil
   135  }
   136  
   137  // FromEntity converts the provided Entity repo-layer representation of an EventDefinition to the service-layer representation model.EventDefinition.
   138  func (c *converter) FromEntity(entity *Entity) *model.EventDefinition {
   139  	return &model.EventDefinition{
   140  		ApplicationID:                repo.StringPtrFromNullableString(entity.ApplicationID),
   141  		ApplicationTemplateVersionID: repo.StringPtrFromNullableString(entity.ApplicationTemplateVersionID),
   142  		PackageID:                    repo.StringPtrFromNullableString(entity.PackageID),
   143  		Name:                         entity.Name,
   144  		Description:                  repo.StringPtrFromNullableString(entity.Description),
   145  		Group:                        repo.StringPtrFromNullableString(entity.GroupName),
   146  		OrdID:                        repo.StringPtrFromNullableString(entity.OrdID),
   147  		LocalTenantID:                repo.StringPtrFromNullableString(entity.LocalTenantID),
   148  		ShortDescription:             repo.StringPtrFromNullableString(entity.ShortDescription),
   149  		SystemInstanceAware:          repo.BoolPtrFromNullableBool(entity.SystemInstanceAware),
   150  		PolicyLevel:                  repo.StringPtrFromNullableString(entity.PolicyLevel),
   151  		CustomPolicyLevel:            repo.StringPtrFromNullableString(entity.CustomPolicyLevel),
   152  		Tags:                         repo.JSONRawMessageFromNullableString(entity.Tags),
   153  		Countries:                    repo.JSONRawMessageFromNullableString(entity.Countries),
   154  		Links:                        repo.JSONRawMessageFromNullableString(entity.Links),
   155  		ReleaseStatus:                repo.StringPtrFromNullableString(entity.ReleaseStatus),
   156  		SunsetDate:                   repo.StringPtrFromNullableString(entity.SunsetDate),
   157  		Successors:                   repo.JSONRawMessageFromNullableString(entity.Successors),
   158  		ChangeLogEntries:             repo.JSONRawMessageFromNullableString(entity.ChangeLogEntries),
   159  		Labels:                       repo.JSONRawMessageFromNullableString(entity.Labels),
   160  		Visibility:                   &entity.Visibility,
   161  		Disabled:                     repo.BoolPtrFromNullableBool(entity.Disabled),
   162  		PartOfProducts:               repo.JSONRawMessageFromNullableString(entity.PartOfProducts),
   163  		LineOfBusiness:               repo.JSONRawMessageFromNullableString(entity.LineOfBusiness),
   164  		Industry:                     repo.JSONRawMessageFromNullableString(entity.Industry),
   165  		Version:                      c.vc.FromEntity(entity.Version),
   166  		Extensible:                   repo.JSONRawMessageFromNullableString(entity.Extensible),
   167  		ResourceHash:                 repo.StringPtrFromNullableString(entity.ResourceHash),
   168  		Hierarchy:                    repo.JSONRawMessageFromNullableString(entity.Hierarchy),
   169  		DocumentationLabels:          repo.JSONRawMessageFromNullableString(entity.DocumentationLabels),
   170  		BaseEntity: &model.BaseEntity{
   171  			ID:        entity.ID,
   172  			Ready:     entity.Ready,
   173  			CreatedAt: entity.CreatedAt,
   174  			UpdatedAt: entity.UpdatedAt,
   175  			DeletedAt: entity.DeletedAt,
   176  			Error:     repo.StringPtrFromNullableString(entity.Error),
   177  		},
   178  	}
   179  }
   180  
   181  // ToEntity converts the provided service-layer representation of an EventDefinition to the repository-layer one.
   182  func (c *converter) ToEntity(eventModel *model.EventDefinition) *Entity {
   183  	visibility := eventModel.Visibility
   184  	if visibility == nil {
   185  		visibility = str.Ptr("public")
   186  	}
   187  
   188  	return &Entity{
   189  		ApplicationID:                repo.NewNullableString(eventModel.ApplicationID),
   190  		ApplicationTemplateVersionID: repo.NewNullableString(eventModel.ApplicationTemplateVersionID),
   191  		PackageID:                    repo.NewNullableString(eventModel.PackageID),
   192  		Name:                         eventModel.Name,
   193  		Description:                  repo.NewNullableString(eventModel.Description),
   194  		GroupName:                    repo.NewNullableString(eventModel.Group),
   195  		OrdID:                        repo.NewNullableString(eventModel.OrdID),
   196  		LocalTenantID:                repo.NewNullableString(eventModel.LocalTenantID),
   197  		ShortDescription:             repo.NewNullableString(eventModel.ShortDescription),
   198  		SystemInstanceAware:          repo.NewNullableBool(eventModel.SystemInstanceAware),
   199  		PolicyLevel:                  repo.NewNullableString(eventModel.PolicyLevel),
   200  		CustomPolicyLevel:            repo.NewNullableString(eventModel.CustomPolicyLevel),
   201  		Tags:                         repo.NewNullableStringFromJSONRawMessage(eventModel.Tags),
   202  		Countries:                    repo.NewNullableStringFromJSONRawMessage(eventModel.Countries),
   203  		Links:                        repo.NewNullableStringFromJSONRawMessage(eventModel.Links),
   204  		ReleaseStatus:                repo.NewNullableString(eventModel.ReleaseStatus),
   205  		SunsetDate:                   repo.NewNullableString(eventModel.SunsetDate),
   206  		Successors:                   repo.NewNullableStringFromJSONRawMessage(eventModel.Successors),
   207  		ChangeLogEntries:             repo.NewNullableStringFromJSONRawMessage(eventModel.ChangeLogEntries),
   208  		Labels:                       repo.NewNullableStringFromJSONRawMessage(eventModel.Labels),
   209  		Visibility:                   *visibility,
   210  		Disabled:                     repo.NewNullableBool(eventModel.Disabled),
   211  		PartOfProducts:               repo.NewNullableStringFromJSONRawMessage(eventModel.PartOfProducts),
   212  		LineOfBusiness:               repo.NewNullableStringFromJSONRawMessage(eventModel.LineOfBusiness),
   213  		Industry:                     repo.NewNullableStringFromJSONRawMessage(eventModel.Industry),
   214  		Version:                      c.convertVersionToEntity(eventModel.Version),
   215  		Extensible:                   repo.NewNullableStringFromJSONRawMessage(eventModel.Extensible),
   216  		ResourceHash:                 repo.NewNullableString(eventModel.ResourceHash),
   217  		Hierarchy:                    repo.NewNullableStringFromJSONRawMessage(eventModel.Hierarchy),
   218  		DocumentationLabels:          repo.NewNullableStringFromJSONRawMessage(eventModel.DocumentationLabels),
   219  		BaseEntity: &repo.BaseEntity{
   220  			ID:        eventModel.ID,
   221  			Ready:     eventModel.Ready,
   222  			CreatedAt: eventModel.CreatedAt,
   223  			UpdatedAt: eventModel.UpdatedAt,
   224  			DeletedAt: eventModel.DeletedAt,
   225  			Error:     repo.NewNullableString(eventModel.Error),
   226  		},
   227  	}
   228  }
   229  
   230  func (c *converter) convertVersionToEntity(inVer *model.Version) version.Version {
   231  	if inVer == nil {
   232  		return version.Version{}
   233  	}
   234  
   235  	return c.vc.ToEntity(*inVer)
   236  }
   237  
   238  func timePtrToTimestampPtr(time *time.Time) *graphql.Timestamp {
   239  	if time == nil {
   240  		return nil
   241  	}
   242  
   243  	t := graphql.Timestamp(*time)
   244  	return &t
   245  }