Skip to content

Report whether a command names its items as a collection - #107

Open
jtnelson wants to merge 2 commits into
developfrom
feature/keep-collection-brackets
Open

jtnelson wants to merge 2 commits into
developfrom
feature/keep-collection-brackets

Conversation

@jtnelson

Copy link
Copy Markdown
Member

Background

Concourse returns a result shaped for a collection whenever a command names a
collection of keys or records, even when the collection holds one item, and a
result for one item when the command names one bare item. CCL writes a
collection with brackets or commas: select [name] from 1 and
select name, age from 1 name a collection of keys, and select name from 1
names one key.

Several parsed commands could not tell the two apart. SelectSymbol,
GetSymbol, NavigateSymbol, BrowseSymbol, the await symbols, and the
read-and-set symbols put a bare key and a bracketed list of one key in the same
keys() collection. CalculateSymbol did the same for its records, and
LinkSymbol for its destinations. So Concourse ran select [name] from 1 as a
select of one key.

Change

Each of those symbols reports whether the command named its items as a
collection, beside the accessor that returns them.

SelectSymbol select = (SelectSymbol) compiler.parse("select [name] from 1")
        .root();
select.keys();                 // [name]
select.hasKeysInCollection();  // true

LinkSymbol link = (LinkSymbol) compiler.parse("link friend from 1 to 2")
        .root();
link.destinations();                 // [2]
link.hasDestinationsInCollection();  // false

The accessors are hasKeysInCollection() on SelectSymbol, GetSymbol,
NavigateSymbol, BrowseSymbol, AwaitSelectSymbol, AwaitGetSymbol,
SelectAndSetSymbol, GetAndSetSymbol, AwaitSelectAndSetSymbol, and
AwaitGetAndSetSymbol, where it describes the keys the command reads;
CalculateSymbol#hasRecordsInCollection(); and
LinkSymbol#hasDestinationsInCollection(). Each symbol gains a constructor
that takes the flag beside each constructor it already has.

The change is additive. The language, every existing constructor, accessor,
equals, and hashCode, and every public member of the generated parser keep
their results, so a caller that does not read the new accessors sees no
difference.

Semantics

  • The flag is true when the command lists its items in brackets, even one
    item as in [name], or lists more than one item, as in name, age. It is
    false for one item without brackets.
  • keys(), records(), and destinations() return the same items for either
    form.
  • equals and hashCode ignore the flag, so [name] and name still parse
    to equal trees for the symbols that implement them.
  • A constructor that does not take the flag sets it when the collection holds
    more than one item.

A parsed command tells a caller whether it names its keys, records or
destinations as a collection, so that `select [name] from 1` and
`select name from 1` parse to symbols a caller can tell apart. A bracketed
list, even of one item, and an unbracketed list of more than one item are
collections; one item without brackets is not.

hasKeysInCollection() covers the keys of SelectSymbol, GetSymbol,
NavigateSymbol and BrowseSymbol and the keys that SelectAndSetSymbol,
GetAndSetSymbol and the await symbols read. CalculateSymbol has
hasRecordsInCollection() and LinkSymbol has hasDestinationsInCollection().
Each symbol has a constructor that takes the flag. The other constructors
keep their signatures and set the flag when there is more than one item.
Every other accessor, equals and hashCode returns the same result for each
form, so [name] and name parse to equal trees.
Grammar#KeyOrKeyCollection() and Grammar#AwaitKeys() keep their signatures
and return only the keys, with null from AwaitKeys() for a bare `where`.
The commands read the keys and whether they form a collection through the
private productions NamedKeyOrKeyCollection() and NamedAwaitKeys(), which
the public ones delegate to, so the parser's public API only gains members.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant