github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/spec/converter.go (about) 1 package spec 2 3 import ( 4 "database/sql" 5 "fmt" 6 7 "github.com/kyma-incubator/compass/components/director/internal/model" 8 "github.com/kyma-incubator/compass/components/director/internal/repo" 9 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 10 "github.com/pkg/errors" 11 ) 12 13 // FetchRequestConverter missing godoc 14 //go:generate mockery --name=FetchRequestConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 15 type FetchRequestConverter interface { 16 ToGraphQL(in *model.FetchRequest) (*graphql.FetchRequest, error) 17 InputFromGraphQL(in *graphql.FetchRequestInput) (*model.FetchRequestInput, error) 18 } 19 20 type converter struct { 21 fr FetchRequestConverter 22 } 23 24 // NewConverter missing godoc 25 func NewConverter(fr FetchRequestConverter) *converter { 26 return &converter{ 27 fr: fr, 28 } 29 } 30 31 // ToGraphQLAPISpec missing godoc 32 func (c *converter) ToGraphQLAPISpec(in *model.Spec) (*graphql.APISpec, error) { 33 if in == nil { 34 return nil, nil 35 } 36 37 if in.ObjectType != model.APISpecReference || in.APIType == nil { 38 return nil, fmt.Errorf("could not convert %s Spec to API Spec with APIType %v", in.ObjectType, in.APIType) 39 } 40 41 var data *graphql.CLOB 42 if in.Data != nil { 43 tmp := graphql.CLOB(*in.Data) 44 data = &tmp 45 } 46 47 return &graphql.APISpec{ 48 ID: in.ID, 49 Data: data, 50 Format: graphql.SpecFormat(in.Format), 51 Type: graphql.APISpecType(*in.APIType), 52 DefinitionID: in.ObjectID, 53 }, nil 54 } 55 56 // ToGraphQLEventSpec missing godoc 57 func (c *converter) ToGraphQLEventSpec(in *model.Spec) (*graphql.EventSpec, error) { 58 if in == nil { 59 return nil, nil 60 } 61 62 if in.ObjectType != model.EventSpecReference || in.EventType == nil { 63 return nil, fmt.Errorf("could not convert %s Spec to Event Spec with EventType %v", in.ObjectType, in.EventType) 64 } 65 66 var data *graphql.CLOB 67 if in.Data != nil { 68 tmp := graphql.CLOB(*in.Data) 69 data = &tmp 70 } 71 72 return &graphql.EventSpec{ 73 ID: in.ID, 74 Data: data, 75 Format: graphql.SpecFormat(in.Format), 76 Type: graphql.EventSpecType(*in.EventType), 77 DefinitionID: in.ObjectID, 78 }, nil 79 } 80 81 // InputFromGraphQLAPISpec missing godoc 82 func (c *converter) InputFromGraphQLAPISpec(in *graphql.APISpecInput) (*model.SpecInput, error) { 83 if in == nil { 84 return nil, nil 85 } 86 87 fetchReq, err := c.fr.InputFromGraphQL(in.FetchRequest) 88 if err != nil { 89 return nil, errors.Wrap(err, "while converting FetchRequest from GraphQL input") 90 } 91 92 apiType := model.APISpecType(in.Type) 93 94 return &model.SpecInput{ 95 Data: (*string)(in.Data), 96 APIType: &apiType, 97 Format: model.SpecFormat(in.Format), 98 FetchRequest: fetchReq, 99 }, nil 100 } 101 102 // InputFromGraphQLEventSpec missing godoc 103 func (c *converter) InputFromGraphQLEventSpec(in *graphql.EventSpecInput) (*model.SpecInput, error) { 104 if in == nil { 105 return nil, nil 106 } 107 108 fetchReq, err := c.fr.InputFromGraphQL(in.FetchRequest) 109 if err != nil { 110 return nil, errors.Wrap(err, "while converting FetchRequest from GraphQL input") 111 } 112 113 eventType := model.EventSpecType(in.Type) 114 115 return &model.SpecInput{ 116 Data: (*string)(in.Data), 117 EventType: &eventType, 118 Format: model.SpecFormat(in.Format), 119 FetchRequest: fetchReq, 120 }, nil 121 } 122 123 // ToEntity missing godoc 124 func (c *converter) ToEntity(in *model.Spec) *Entity { 125 refID := repo.NewValidNullableString(in.ObjectID) 126 127 var apiDefID sql.NullString 128 var apiSpecFormat sql.NullString 129 var apiSpecType sql.NullString 130 131 var eventAPIDefID sql.NullString 132 var eventSpecFormat sql.NullString 133 var eventSpecType sql.NullString 134 135 switch in.ObjectType { 136 case model.APISpecReference: 137 apiDefID = refID 138 apiSpecFormat = repo.NewValidNullableString(string(in.Format)) 139 apiSpecType = repo.NewValidNullableString(string(*in.APIType)) 140 case model.EventSpecReference: 141 eventAPIDefID = refID 142 eventSpecFormat = repo.NewValidNullableString(string(in.Format)) 143 eventSpecType = repo.NewValidNullableString(string(*in.EventType)) 144 } 145 146 return &Entity{ 147 ID: in.ID, 148 APIDefID: apiDefID, 149 EventAPIDefID: eventAPIDefID, 150 SpecData: repo.NewNullableString(in.Data), 151 APISpecFormat: apiSpecFormat, 152 APISpecType: apiSpecType, 153 EventSpecFormat: eventSpecFormat, 154 EventSpecType: eventSpecType, 155 CustomType: repo.NewNullableString(in.CustomType), 156 } 157 } 158 159 // FromEntity missing godoc 160 func (c *converter) FromEntity(in *Entity) (*model.Spec, error) { 161 objectID, objectType, err := c.objectReferenceFromEntity(*in) 162 if err != nil { 163 return nil, errors.Wrap(err, "while determining object reference") 164 } 165 166 var apiSpecFormat model.SpecFormat 167 var apiSpecType *model.APISpecType 168 169 var eventSpecFormat model.SpecFormat 170 var eventSpecType *model.EventSpecType 171 172 apiSpecFormatStr := repo.StringPtrFromNullableString(in.APISpecFormat) 173 if apiSpecFormatStr != nil { 174 apiSpecFormat = model.SpecFormat(*apiSpecFormatStr) 175 } 176 177 apiSpecTypeStr := repo.StringPtrFromNullableString(in.APISpecType) 178 if apiSpecTypeStr != nil { 179 apiType := model.APISpecType(*apiSpecTypeStr) 180 apiSpecType = &apiType 181 } 182 183 eventSpecFormatStr := repo.StringPtrFromNullableString(in.EventSpecFormat) 184 if eventSpecFormatStr != nil { 185 eventSpecFormat = model.SpecFormat(*eventSpecFormatStr) 186 } 187 188 eventSpecTypeStr := repo.StringPtrFromNullableString(in.EventSpecType) 189 if eventSpecTypeStr != nil { 190 eventType := model.EventSpecType(*eventSpecTypeStr) 191 eventSpecType = &eventType 192 } 193 194 specFormat := apiSpecFormat 195 if objectType == model.EventSpecReference { 196 specFormat = eventSpecFormat 197 } 198 199 return &model.Spec{ 200 ID: in.ID, 201 ObjectType: objectType, 202 ObjectID: objectID, 203 Data: repo.StringPtrFromNullableString(in.SpecData), 204 Format: specFormat, 205 APIType: apiSpecType, 206 EventType: eventSpecType, 207 CustomType: repo.StringPtrFromNullableString(in.CustomType), 208 }, nil 209 } 210 211 func (c *converter) objectReferenceFromEntity(in Entity) (string, model.SpecReferenceObjectType, error) { 212 if in.APIDefID.Valid { 213 return in.APIDefID.String, model.APISpecReference, nil 214 } 215 216 if in.EventAPIDefID.Valid { 217 return in.EventAPIDefID.String, model.EventSpecReference, nil 218 } 219 220 return "", "", fmt.Errorf("incorrect Object Reference ID and its type for Entity with ID '%s'", in.ID) 221 }