Skip to content

Commit

Permalink
Merge pull request #152 from MUzairS15/MUzairS15/enhanced-converter
Browse files Browse the repository at this point in the history
Handle invalid values in older designs during conversion effectively.
  • Loading branch information
Mohd Uzair authored Aug 26, 2024
2 parents 424fb69 + 8caaac3 commit 38d154c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/layer5io/meshkit v0.7.50
github.com/oapi-codegen/runtime v1.1.1
github.com/pkg/errors v0.9.1
gonum.org/v1/gonum v0.15.0
gorm.io/gorm v1.25.11
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gonum.org/v1/gonum v0.15.0 h1:2lYxjRbTYyxkJxlhC+LvJIx3SsANPdRybu1tGj9/OrQ=
gonum.org/v1/gonum v0.15.0/go.mod h1:xzZVBJBtS+Mz4q0Yl2LJTk+OxOg4jiXZ7qBoM0uISGo=
google.golang.org/api v0.152.0 h1:t0r1vPnfMc260S2Ci+en7kfCZaLOPs5KI0sVV/6jZrY=
google.golang.org/api v0.152.0/go.mod h1:3qNJX5eOmhiWYc67jRA/3GsDw97UFb5ivv7Y2PrriAY=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
50 changes: 42 additions & 8 deletions models/v1beta1/pattern/design_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package pattern
import (
"crypto/rand"
"math/big"
"reflect"
"strings"

"github.com/gofrs/uuid"
"github.com/layer5io/meshkit/utils"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/meshery/schemas/models/v1beta1/component"
"github.com/meshery/schemas/models/v1beta1/model"
"github.com/pkg/errors"
"gonum.org/v1/gonum/graph/formats/cytoscapejs"
)

// The pattern file indicated by "p", is converted to the version pointed by "pattern", the version of the patternFile which implements the Hub interface indicates the version the conversion will happen.
Expand Down Expand Up @@ -113,24 +116,49 @@ func (p *PatternFile) convertFromTraits(cmp *component.ComponentDefinition, serv
}

// Handle node id: traits.meshmap.id
compNodeID, err := utils.Cast[string](extensionsMetadata["id"])
if err != nil {
return errors.Wrapf(err, "failed to extract node id for the component \"%s\" of type \"%s\"", cmp.DisplayName, cmp.Component.Kind)
}
compNodeUUID, _ := uuid.NewV4()

compNodeUUID, err := uuid.FromString(compNodeID)
if err != nil {
return errors.Wrapf(err, "failed to convert node id \"%s\" for the component \"%s\" of type \"%s\", to uuid.", compNodeID, cmp.DisplayName, cmp.Component.Kind)
compNodeID, ok := extensionsMetadata["id"].(string)
if ok {
nodeID, err := uuid.FromString(compNodeID)
if err == nil {
compNodeUUID = nodeID
}
}

isNamespaced := false
cmp.Id = compNodeUUID
_metadata := extensionsMetadata["meshmodel-metadata"]
metadata := map[string]interface{}{}
if _metadata != nil {
metadata, err = utils.Cast[map[string]interface{}](_metadata)
if err != nil {
return errors.Wrapf(err, "unable to extract metadata for the component \"%s\" of type \"%s\" from the design file", cmp.DisplayName, cmp.Component.Kind)
}

namespaced, ok := metadata["isNamespaced"]
if ok {
reflectType := reflect.TypeOf(namespaced)
if reflectType.Kind() == reflect.String {
val, _ := namespaced.(string)
if strings.ToLower(val) == "true" {
isNamespaced = true
}
} else if reflectType.Kind() == reflect.Bool {
isNamespaced, _ = namespaced.(bool)
}
}
}

delete(metadata, "isNamespaced")
// Handle component metadata: traits.meshmap.meshmodel-metadata
_compMetadata, err := utils.MarshalAndUnmarshal[interface{}, component.ComponentDefinition_Metadata](extensionsMetadata["meshmodel-metadata"])
_compMetadata, err := utils.MarshalAndUnmarshal[map[string]interface{}, component.ComponentDefinition_Metadata](metadata)
if err != nil {
return errors.Wrapf(err, "unable to extract metadata for the component \"%s\" of type \"%s\" from the design file", cmp.DisplayName, cmp.Component.Kind)
}

cmp.Metadata = _compMetadata
cmp.Metadata.IsNamespaced = isNamespaced

// Handle position properties: traits.meshmap.position
randX, _ := rand.Int(rand.Reader, big.NewInt(100))
Expand All @@ -150,6 +178,12 @@ func (p *PatternFile) convertFromTraits(cmp *component.ComponentDefinition, serv
},
}

pos, ok := extensionsMetadata["position"].(cytoscapejs.Position)
if ok {
cmp.Styles.Position.X, _ = big.NewFloat(pos.X).Float32()
cmp.Styles.Position.Y, _ = big.NewFloat(pos.Y).Float32()
}

cmp.Metadata.AdditionalProperties = make(map[string]interface{}, 0)
// Handle position properties: service.dependsOn/
cmp.Metadata.AdditionalProperties["dependsOn"] = service.DependsOn
Expand Down

0 comments on commit 38d154c

Please sign in to comment.