Skip to content

Commit

Permalink
insert the objects to map to array to remove the duplicate mapping code
Browse files Browse the repository at this point in the history
  • Loading branch information
itamar-smirra-port committed May 5, 2024
1 parent b217def commit 02877a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
36 changes: 17 additions & 19 deletions pkg/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,42 +244,40 @@ func (c *Controller) getObjectEntities(obj interface{}, selector port.Selector,
}

entities := make([]port.Entity, 0, len(mappings))
if itemsToParse != "" {
objectsToMap := make([]interface{}, 0)

if (itemsToParse == "") {
objectsToMap = append(objectsToMap, structuredObj)
} else {
items, parseItemsError := jq.ParseArray(itemsToParse, structuredObj)
if parseItemsError != nil {
return nil, parseItemsError
}
editedObject, ok := structuredObj.(map[string]interface{})

mappedObject, ok := structuredObj.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("error parsing object '%#v'", structuredObj)
}

for _, item := range items {
editedObject["item"] = item
selectorResult, err := isPassSelector(editedObject, selector)

if err != nil {
return nil, err
}

if selectorResult {
currentEntities, err := mapEntities(editedObject, mappings)
if err != nil {
return nil, err
}

entities = append(entities, currentEntities...)
copiedObject := make(map[string]interface{})
for key, value := range mappedObject {
copiedObject[key] = value
}
copiedObject["item"] = item
objectsToMap = append(objectsToMap, copiedObject)
}
} else {
selectorResult, err := isPassSelector(structuredObj, selector)
}

for _, objectToMap := range objectsToMap {
selectorResult, err := isPassSelector(objectToMap, selector)

if err != nil {
return nil, err
}

if selectorResult {
currentEntities, err := mapEntities(structuredObj, mappings)
currentEntities, err := mapEntities(objectToMap, mappings)
if err != nil {
return nil, err
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/port/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,32 +179,32 @@ type ResponseBody struct {
}

type EntityMapping struct {
Identifier string `json:"identifier"`
Title string `json:"title"`
Blueprint string `json:"blueprint"`
Icon string `json:"icon,omitempty"`
Team string `json:"team,omitempty"`
Properties map[string]string `json:"properties,omitempty"`
Relations map[string]string `json:"relations,omitempty"`
Identifier string `json:"identifier" yaml:"identifier"`
Title string `json:"title" yaml:"title"`
Blueprint string `json:"blueprint" yaml:"blueprint"`
Icon string `json:"icon,omitempty" yaml:"icon,omitempty"`
Team string `json:"team,omitempty" yaml:"team,omitempty"`
Properties map[string]string `json:"properties,omitempty" yaml:"properties,omitempty"`
Relations map[string]string `json:"relations,omitempty" yaml:"relations,omitempty"`
}

type EntityMappings struct {
Mappings []EntityMapping `json:"mappings"`
Mappings []EntityMapping `json:"mappings" yaml:"mappings"`
}

type Port struct {
Entity EntityMappings `json:"entity"`
ItemsToParse string `json:"itemsToParse"`
Entity EntityMappings `json:"entity" yaml:"entity"`
ItemsToParse string `json:"itemsToParse" yaml:"itemsToParse"`
}

type Selector struct {
Query string
}

type Resource struct {
Kind string `json:"kind"`
Selector Selector `json:"selector,omitempty"`
Port Port `json:"port"`
Kind string `json:"kind" yaml:"kind"`
Selector Selector `json:"selector,omitempty" yaml:"selector,omitempty"`
Port Port `json:"port" yaml:"port"`
}

type EventListenerSettings struct {
Expand Down

0 comments on commit 02877a2

Please sign in to comment.