github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/formationassignment/converter.go (about) 1 package formationassignment 2 3 import ( 4 "encoding/json" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/str" 7 8 "github.com/pkg/errors" 9 10 "github.com/kyma-incubator/compass/components/director/internal/model" 11 "github.com/kyma-incubator/compass/components/director/internal/repo" 12 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 13 ) 14 15 // NewConverter creates a new formation assignment converter 16 func NewConverter() *converter { 17 return &converter{} 18 } 19 20 type converter struct{} 21 22 // ToGraphQL converts from internal model to GraphQL output 23 func (c *converter) ToGraphQL(in *model.FormationAssignment) (*graphql.FormationAssignment, error) { 24 if in == nil { 25 return nil, nil 26 } 27 28 var strValue *string 29 if in.Value != nil { 30 marshalledValue, err := json.Marshal(in.Value) 31 if err != nil { 32 return nil, errors.Wrap(err, "while converting formation assignment to GraphQL") 33 } 34 strValue = str.Ptr(string(marshalledValue)) 35 } 36 37 return &graphql.FormationAssignment{ 38 ID: in.ID, 39 Source: in.Source, 40 SourceType: graphql.FormationAssignmentType(in.SourceType), 41 Target: in.Target, 42 TargetType: graphql.FormationAssignmentType(in.TargetType), 43 State: in.State, 44 Value: strValue, 45 }, nil 46 } 47 48 // MultipleToGraphQL converts multiple internal models to GraphQL models 49 func (c *converter) MultipleToGraphQL(in []*model.FormationAssignment) ([]*graphql.FormationAssignment, error) { 50 if in == nil { 51 return nil, nil 52 } 53 formationAssignment := make([]*graphql.FormationAssignment, 0, len(in)) 54 for _, r := range in { 55 if r == nil { 56 continue 57 } 58 59 fa, err := c.ToGraphQL(r) 60 if err != nil { 61 return nil, err 62 } 63 64 formationAssignment = append(formationAssignment, fa) 65 } 66 67 return formationAssignment, nil 68 } 69 70 // ToInput converts from internal model to internal model input 71 func (c *converter) ToInput(assignment *model.FormationAssignment) *model.FormationAssignmentInput { 72 if assignment == nil { 73 return nil 74 } 75 76 return &model.FormationAssignmentInput{ 77 FormationID: assignment.FormationID, 78 Source: assignment.Source, 79 SourceType: assignment.SourceType, 80 Target: assignment.Target, 81 TargetType: assignment.TargetType, 82 State: assignment.State, 83 Value: assignment.Value, 84 } 85 } 86 87 // FromInput converts from internal model input to internal model 88 func (c *converter) FromInput(in *model.FormationAssignmentInput) *model.FormationAssignment { 89 if in == nil { 90 return nil 91 } 92 93 return &model.FormationAssignment{ 94 FormationID: in.FormationID, 95 Source: in.Source, 96 SourceType: in.SourceType, 97 Target: in.Target, 98 TargetType: in.TargetType, 99 State: in.State, 100 Value: in.Value, 101 } 102 } 103 104 // ToEntity converts from internal model to entity 105 func (c *converter) ToEntity(in *model.FormationAssignment) *Entity { 106 if in == nil { 107 return nil 108 } 109 110 return &Entity{ 111 ID: in.ID, 112 FormationID: in.FormationID, 113 TenantID: in.TenantID, 114 Source: in.Source, 115 SourceType: string(in.SourceType), 116 Target: in.Target, 117 TargetType: string(in.TargetType), 118 State: in.State, 119 Value: repo.NewNullableStringFromJSONRawMessage(in.Value), 120 } 121 } 122 123 // FromEntity converts from entity to internal model 124 func (c *converter) FromEntity(e *Entity) *model.FormationAssignment { 125 if e == nil { 126 return nil 127 } 128 129 return &model.FormationAssignment{ 130 ID: e.ID, 131 FormationID: e.FormationID, 132 TenantID: e.TenantID, 133 Source: e.Source, 134 SourceType: model.FormationAssignmentType(e.SourceType), 135 Target: e.Target, 136 TargetType: model.FormationAssignmentType(e.TargetType), 137 State: e.State, 138 Value: repo.JSONRawMessageFromNullableString(e.Value), 139 } 140 }