Skip to content

Commit

Permalink
feat(cli) multiple attrs for link cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
helderbetiol committed Feb 29, 2024
1 parent 4ffc2fd commit 1cde8e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
17 changes: 15 additions & 2 deletions CLI/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,9 @@ func (n *cameraWaitNode) execute() (interface{}, error) {
type linkObjectNode struct {
source node
destination node
attr string
attrs []string
values []node
slots []node
}

func (n *linkObjectNode) execute() (interface{}, error) {
Expand All @@ -1538,7 +1539,19 @@ func (n *linkObjectNode) execute() (interface{}, error) {
values = append(values, val)
}

return nil, cmd.LinkObject(source, dest, n.attr, values)
var slots []string
if n.slots != nil {
slots = []string{}
for _, node := range n.slots {
str, err := nodeToString(node, "slots")
slots = append(slots, str)
if err != nil {
return nil, err
}
}
}

return nil, cmd.LinkObject(source, dest, n.attrs, values, slots)
}

type unlinkObjectNode struct {
Expand Down
21 changes: 9 additions & 12 deletions CLI/controllers/commandController.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func FocusUI(path string) error {
return nil
}

func LinkObject(source string, destination string, attributeName string, values []any) error {
func LinkObject(source string, destination string, attrs []string, values []any, slots []string) error {
sourceUrl, err := C.ObjectUrl(source, 0)
if err != nil {
return err
Expand All @@ -591,18 +591,15 @@ func LinkObject(source string, destination string, attributeName string, values
return fmt.Errorf("only stray objects can be linked")
}
payload := map[string]any{"parentId": destPath.ObjectID}
if attributeName != "" {
if attributeName == "slot" {
slots := []string{}
for _, value := range values {
slots = append(slots, value.(string))
}
value := "[" + strings.Join(slots, ",") + "]"
payload["slot"] = value
} else {
payload[attributeName] = values[0]
}

for i, attr := range attrs {
payload[attr] = values[i]
}

if slots != nil {
payload["slot"] = "[" + strings.Join(slots, ",") + "]"
}

_, err = API.Request("PATCH", sourceUrl+"/link", payload, http.StatusOK)
if err != nil {
return err
Expand Down
13 changes: 8 additions & 5 deletions CLI/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,21 +882,24 @@ func (p *parser) parseLink() node {
sourcePath := p.parsePath("source path")
p.expect("@")
destPath := p.parsePath("destination path")
if p.parseExact("@") {
values := []node{}
attrs := []string{}
var slots []node
for p.parseExact("@") {
p.skipWhiteSpaces()
attr := p.parseComplexWord("attribute")
p.skipWhiteSpaces()
p.expect("=")
p.skipWhiteSpaces()
values := []node{}
if attr == "slot" {
values = p.parseStringOrVecStr("slot")
slots = p.parseStringOrVecStr("slot")
} else {
value := p.parseValue()
values = append(values, value)
attrs = append(attrs, attr)
}
return &linkObjectNode{sourcePath, destPath, attr, values}
}
return &linkObjectNode{sourcePath, destPath, "", nil}
return &linkObjectNode{sourcePath, destPath, attrs, values, slots}
}

func (p *parser) parseUnlink() node {
Expand Down

0 comments on commit 1cde8e6

Please sign in to comment.