This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
description_entity.go
94 lines (82 loc) · 3.03 KB
/
description_entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package transformers
import (
"context"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flytestdlib/logger"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
)
// CreateDescriptionEntityModel Transforms a TaskCreateRequest to a Description entity model
func CreateDescriptionEntityModel(
descriptionEntity *admin.DescriptionEntity,
id core.Identifier) (*models.DescriptionEntity, error) {
ctx := context.Background()
if descriptionEntity == nil {
return nil, nil
}
var longDescriptionBytes []byte
var sourceCode models.SourceCode
var err error
if descriptionEntity.LongDescription != nil {
longDescriptionBytes, err = proto.Marshal(descriptionEntity.LongDescription)
if err != nil {
logger.Errorf(ctx, "Failed to marshal LongDescription with error: %v", err)
return nil, err
}
}
if descriptionEntity.LongDescription != nil {
longDescriptionBytes, err = proto.Marshal(descriptionEntity.LongDescription)
if err != nil {
logger.Errorf(ctx, "Failed to marshal LongDescription with error: %v", err)
return nil, err
}
}
if descriptionEntity.SourceCode != nil {
sourceCode = models.SourceCode{Link: descriptionEntity.SourceCode.Link}
}
return &models.DescriptionEntity{
DescriptionEntityKey: models.DescriptionEntityKey{
ResourceType: id.ResourceType,
Project: id.Project,
Domain: id.Domain,
Name: id.Name,
Version: id.Version,
},
ShortDescription: descriptionEntity.ShortDescription,
LongDescription: longDescriptionBytes,
SourceCode: sourceCode,
}, nil
}
func FromDescriptionEntityModel(descriptionEntityModel models.DescriptionEntity) (*admin.DescriptionEntity, error) {
longDescription := admin.Description{}
err := proto.Unmarshal(descriptionEntityModel.LongDescription, &longDescription)
if err != nil {
return nil, errors.NewFlyteAdminError(codes.Internal, "failed to unmarshal longDescription")
}
return &admin.DescriptionEntity{
Id: &core.Identifier{
ResourceType: descriptionEntityModel.ResourceType,
Project: descriptionEntityModel.Project,
Domain: descriptionEntityModel.Domain,
Name: descriptionEntityModel.Name,
Version: descriptionEntityModel.Version,
},
ShortDescription: descriptionEntityModel.ShortDescription,
LongDescription: &longDescription,
SourceCode: &admin.SourceCode{Link: descriptionEntityModel.Link},
}, nil
}
func FromDescriptionEntityModels(descriptionEntityModels []models.DescriptionEntity) ([]*admin.DescriptionEntity, error) {
descriptionEntities := make([]*admin.DescriptionEntity, len(descriptionEntityModels))
for idx, descriptionEntityModel := range descriptionEntityModels {
descriptionEntity, err := FromDescriptionEntityModel(descriptionEntityModel)
if err != nil {
return nil, err
}
descriptionEntities[idx] = descriptionEntity
}
return descriptionEntities, nil
}