Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getListItem and getListItems don't work with variables #22

Closed
mikeogezi opened this issue Oct 30, 2023 · 4 comments
Closed

getListItem and getListItems don't work with variables #22

mikeogezi opened this issue Oct 30, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@mikeogezi
Copy link
Contributor

mikeogezi commented Oct 30, 2023

Functions getListItem and getListItems don't work with variables, however, the underlying action supports this functionality.

Here is some code that produces the error:

@names = list("John", "Jane", "Joe")
@names_len = count(names)

@index = 0
@index_plus_1 = 1
@seen_names
repeat i for names_len {
  @index += 1
  @index_plus_1 += 1
  @seen_names += getListItems(names, index, index_plus_1)
}

Here is the error:

Parsing main.cherri...

Error: strconv.ParseInt: parsing "index": invalid syntax

###################
#   DEBUG PANIC   #
###################

### PARSING ###

Previous Character:
) 15:0

Current Character:
LF 15:0

Next Character:
} 15:0

Current Line: 
}
## TOKENS ##
1 | {variable names action {list [{" John} {" Jane} {" Joe}]}}
2 | {variable names_len action {count [{variable names}]}}
3 | {variable index 0123456789.- 0}
4 | {variable index_plus_1 0123456789.- 1}
5 | {variable seen_names  <nil>}
6 | {repeat  205158AF-02DB-4E06-8149-ADEB37257AC2 variable names_len}
7 | {variable i variable Repeat Index}
8 | {+= index 0123456789.- 1}
9 | {+= index_plus_1 0123456789.- 1}

## TOKEN CHARS ##
map[#define:[35 100 101 102 105 110 101] #import:[35 105 109 112 111 114 116] #question:[35 113 117 101 115 116 105 111 110] +=:[43 61] const:[99 111 110 115 116] false:[102 97 108 115 101] for :[102 111 114 32] nil:[110 105 108] repeat :[114 101 112 101 97 116 32] true:[116 114 117 101]]

## DEFINITIONS ##
Name: main
Color: -1263359489
Glyph: 61440
Inputs: []
Outputs: []
Workflows: []
No Input: { []}
macOS Only: false
Mininum Version: 900
iOS Version: 17.0

## VARIABLES ##
@index (Variable) = 0 (0123456789.-)
@index_plus_1 (Variable) = 1 (0123456789.-)
@seen_names (Variable) = (empty)
@i (Variable) = i (") (repeat item var)
@names (Variable) = {list [{" John} {" Jane} {" Joe}]} (action)
@names_len (Variable) = {count [{variable names}]} (action)

## MENUS ##
map[]

## IMPORT QUESTIONS ##
map[]

### PLIST GEN ###

## UUIDS ##
map[]

### CUSTOM ACTIONS ###


### INCLUDES ###

## INCLUDED ##
[]

## INCLUDES MAP ##
[]

#############################################################

panic: debug

goroutine 1 [running]:
main.panicDebug()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/main.go:265 +0x10c
main.handle({0x104bcdfc8?, 0x1400011c5d0?})
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/main.go:211 +0x168
main.incrementValue({0x104ba5820?, 0x14000118190?})
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/action.go:378 +0x54
main.scriptingActions.func51({0x14000164300, 0x3, 0x14000126480?})
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/actions_std.go:3774 +0x3c
main.checkAction()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/action.go:206 +0xb4
main.collectAction()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/parser.go:1133 +0x1cc
main.collectValue(0x1400018da68, 0x1400019da78, 0xa)
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/parser.go:350 +0x320
main.collectVariableValue(0x0, 0x1400019da68, 0x1400019da78, 0x1400018da58, 0x1400019da98, 0x1400019da88)
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/parser.go:290 +0x1bc
main.collectVariable(0x0)
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/parser.go:503 +0x330
main.parse()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/parser.go:169 +0x13c
main.initParse()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/parser.go:50 +0x348
main.main()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.10-beta.0.20231029101725-fb1cbb06a57f/main.go:62 +0x258```
@electrikmilk electrikmilk self-assigned this Oct 30, 2023
@electrikmilk electrikmilk added the bug Something isn't working label Oct 30, 2023
@electrikmilk
Copy link
Owner

electrikmilk commented Oct 30, 2023

This error is related to getListItems() trying to use index as a number and due to Shortcuts' counting starting at 1, not 0 we try to increment the value of index so that the programmer can start at 0 like they'd be used to in other languages.

But index is not a number after adding to it, because this syntax does not work how it does in other languages.

@index = 0
@index += 1

Using += currently creates an Add to Variable action, which adds 1 to index, but by turning index into an array. So afterward the value of index is [0,1]. We don't detect this as we currently do not change the type of the variable to an array when it's added to.

This is very confusing syntax. If you are adding to a number using this syntax, it should increment index instead of converting it to an array. This problem also affects strings, it also creates an array instead of appending to the string variable.

Work on this issue will focus on refactoring this syntax to do the appropriate thing based on the type of the variable being added to, increment numbers, append to text, and add to an array if it was originally an array.

This also brought up the fact that a panic currently looks like something in the code is wrong when this was actually a program error, that is now fixed.

Thank you for calling this out!

@electrikmilk electrikmilk added enhancement New feature or request and removed bug Something isn't working labels Nov 1, 2023
electrikmilk added a commit that referenced this issue Nov 1, 2023
This fixes instances where += syntax is used on a number or string.

In the case of an integer, +=, -=, *= and /= may be used to modify the
variable with a literal or variable using the Calculate action.

In the case of a string, we reassign the variable using a text action to
append the variable or literal to the end of the original variable.

Uninitialized values will be added to using the type of variable that is
being added to them.

Arrays will continue to create a 'Add to Variable' action to add
whatever value to that variable.
@electrikmilk
Copy link
Owner

Closed by ad1c73b

@mikeogezi
Copy link
Contributor Author

mikeogezi commented Dec 5, 2023

@electrikmilk, this issue still exists:

@list = list("Device Name", "Device Hostname")
@index = 0
@name = getListItem(list, index)
alert(name)

I get a similar panic trying to build this.

Full stack trace:

Program panic :(

Please report this: https://github.com/electrikmilk/cherri/issues/new

panic: strconv.ParseInt: parsing "index": invalid syntax

goroutine 1 [running]:
main.handle({0x1007197a8, 0x1400009c5d0})
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/main.go:273 +0x198
main.incrementValue({0x1006f1600?, 0x14000098110?})
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/action.go:391 +0x54
main.scriptingActions.func50({0x140000bc200, 0x1400009c3c0?, 0x140000a6790?})
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/actions_std.go:3873 +0x38
main.checkAction()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/action.go:202 +0x7c
main.collectAction()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/parser.go:1189 +0x270
main.collectValue(0x1400019b7b0, 0x1400019b7c0, 0xa)
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/parser.go:307 +0x4e4
main.collectVariableValue(0x0, 0x1400019b7b0, 0x1400019b7c0, 0x1400019b7f0, 0x1400019b7e0)
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/parser.go:240 +0x3c
main.collectVariable(0x0)
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/parser.go:485 +0x6e8
main.parse()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/parser.go:147 +0x480
main.initParse()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/parser.go:49 +0x2f4
main.main()
        /Users/michaelogezi/go/pkg/mod/github.com/electrikmilk/cherri@v0.1.11-beta.0.20231203021550-d1a03f308510/main.go:75 +0x35c

@mikeogezi mikeogezi changed the title Functions getListItem and getListItems don't work with variables getListItem and getListItems don't work with variables Dec 6, 2023
@electrikmilk
Copy link
Owner

We shouldn't try to increment the value if the value type of the argument is a variable. This comes with the disadvantage of having to start counting at 1. This is fixed by cb4343a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants