github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/label/converter.go (about) 1 package label 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 7 "github.com/kyma-incubator/compass/components/director/internal/repo" 8 9 "github.com/kyma-incubator/compass/components/director/internal/model" 10 "github.com/pkg/errors" 11 ) 12 13 // NewConverter missing godoc 14 func NewConverter() *converter { 15 return &converter{} 16 } 17 18 type converter struct{} 19 20 // ToEntity missing godoc 21 func (c *converter) ToEntity(in *model.Label) (*Entity, error) { 22 var valueMarshalled []byte 23 var err error 24 25 if in.Value != nil { 26 valueMarshalled, err = json.Marshal(in.Value) 27 if err != nil { 28 return nil, errors.Wrap(err, "while marshalling Value") 29 } 30 } 31 32 var appID sql.NullString 33 var rtmID sql.NullString 34 var rtmCtxID sql.NullString 35 var appTmplID sql.NullString 36 switch in.ObjectType { 37 case model.ApplicationLabelableObject: 38 appID = sql.NullString{ 39 Valid: true, 40 String: in.ObjectID, 41 } 42 case model.RuntimeLabelableObject: 43 rtmID = sql.NullString{ 44 Valid: true, 45 String: in.ObjectID, 46 } 47 case model.RuntimeContextLabelableObject: 48 rtmCtxID = sql.NullString{ 49 Valid: true, 50 String: in.ObjectID, 51 } 52 case model.AppTemplateLabelableObject: 53 appTmplID = sql.NullString{ 54 Valid: true, 55 String: in.ObjectID, 56 } 57 } 58 59 return &Entity{ 60 ID: in.ID, 61 TenantID: repo.NewNullableString(in.Tenant), 62 AppID: appID, 63 RuntimeID: rtmID, 64 RuntimeContextID: rtmCtxID, 65 AppTemplateID: appTmplID, 66 Key: in.Key, 67 Value: string(valueMarshalled), 68 Version: in.Version, 69 }, nil 70 } 71 72 // FromEntity missing godoc 73 func (c *converter) FromEntity(in *Entity) (*model.Label, error) { 74 var valueUnmarshalled interface{} 75 if in.Value != "" { 76 err := json.Unmarshal([]byte(in.Value), &valueUnmarshalled) 77 if err != nil { 78 return nil, errors.Wrap(err, "while unmarshalling Value") 79 } 80 } 81 82 var objectType model.LabelableObject 83 var objectID string 84 85 if in.AppID.Valid { 86 objectID = in.AppID.String 87 objectType = model.ApplicationLabelableObject 88 } else if in.RuntimeID.Valid { 89 objectID = in.RuntimeID.String 90 objectType = model.RuntimeLabelableObject 91 } else if in.RuntimeContextID.Valid { 92 objectID = in.RuntimeContextID.String 93 objectType = model.RuntimeContextLabelableObject 94 } else if in.AppTemplateID.Valid { 95 objectID = in.AppTemplateID.String 96 objectType = model.AppTemplateLabelableObject 97 } 98 99 return &model.Label{ 100 ID: in.ID, 101 Tenant: repo.StringPtrFromNullableString(in.TenantID), 102 ObjectID: objectID, 103 ObjectType: objectType, 104 Key: in.Key, 105 Value: valueUnmarshalled, 106 Version: in.Version, 107 }, nil 108 }