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

Struct literal - Revist #286

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 46 additions & 24 deletions versions/development/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ Table of Contents
* [Struct Declarations](#struct-declarations)
* [Optional and non Empty Struct Values](#optional-and-non-empty-struct-values)
* [Using a Struct](#using-a-struct)
* [Struct Assignment from Map Literal](#struct-assignment-from-map-literal)
* [Struct Member Access](#struct-member-access)
* [Importing Structs](#importing-structs)
* [Struct Literals](#struct-literals)
* [Struct Member Access](#struct-member-access)
* [Importing Structs](#importing-structs)
* [Namespaces](#namespaces)
* [Scope](#scope)
* [Optional Parameters & Type Constraints](#optional-parameters--type-constraints)
Expand Down Expand Up @@ -328,8 +328,7 @@ struct BamAndIndex {
File bam
File bam_index
}
BamAndIndex b_and_i = {"bam":"NA12878.bam", "bam_index":"NA12878.bam.bai"}
BamAndIndex b_and_i_2 = object {bam:"NA12878.bam", bam_index:"NA12878.bam.bai"}
BamAndIndex b_and_i = BamAndIndex(bam="NA12878.bam",bam_index="NA12878.bam.bai")
```

Some examples of types:
Expand Down Expand Up @@ -680,23 +679,6 @@ String a = "one"
Map[String, Int] = {a: 1, "not " + a: 2}
```

### Object Literals

Similar to Map literals, however object literal keys are unquoted strings.
This makes them well suited for assigning to `Structs`.
Beware the behaviour difference with Map literals

```
Map[String, Int] map_1 = object {a: 1, b: 2}
String a = "one"
String b = "two"
# map_2 != map_1
Map[String, Int] map_2 = {a: 1, b: 2}
```

map_1 has the keys 'a' and 'b'.
map_2 has the keys 'one' and 'two'.

### Pair Literals

Pair values can be specified inside of a WDL using another Python-like syntax, as follows:
Expand Down Expand Up @@ -2069,7 +2051,7 @@ struct Name {

When using a struct in the declaration section of either a `workflow` or a `task` or `output` section you define them in the same way you would define any other type.

Structs should be declared with Object literals, as the keys can be checked
Structs should be declared with Struct literals, as the keys can be checked
for correctness before run time.
Assignment is also possible from `Maps`, other `Structs`.
As `Map` literals can contain arbitrary expressions for the keys,
Expand Down Expand Up @@ -2102,14 +2084,54 @@ task myTask {
}

workflow myWorkflow {
Person harry = object {name: "Harry", age: 11}
Person harry = Person(name="Harry", age=11}
call myTask {
input:
a = harry
}
}
```

#### Struct Literals
Structs can be created and assigned using the `Struct Literal` notation. Struct literal notation enables the creation of typed struct objects which enforces the typing of all of is
assigned parameters. Struct literal notation attempts to be more declarative to help engine implementations apply the proper
type conversions to nested structs, as well as remove any ambiguity over what the object being constructed represents.

A `Struct Literal` declaration looks like an engine function call, where instead of a function the name of the struct is used followed by parenthesis. For example:

```wdl




Person( ... )
```

Arugments placed within the parethesis are key-value pairs, where the key is the name of one of the struct declarations, and the value is the value to set the argument to.
There is no need to wrap a key in quotation marks, instead keys are represented in plain text followed by an equals sign `=`. The value follows after the `=`. Multiple arguments can
be separated by a comma ',' and arguments do not need to be specified in a specific order.

Values passed to struct literals can be any previously defined declaration, or they themselves can be a literal notation.


```wdl

#Simple case
File fastq_1
File fastq_2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a typo presumably? I think it should either be (a) assigned a value (even File fastq2 = ... as a placeholder) or put into an input { } section

Sample sample_1 = Sample( type="Blood", sequencing_info="WGS", fastq=fastq_1 )
Sample sample_2 = Sample( type="Liver", sequencing_info="WES", fastq=fastq_2 )
Person person_1 = Person( name="John", age=30, samples=[sample_1,sample_2] )

#Example representing using different literal notations
SomeStruct struct_1 = SomeStruct(someDict={"key":"value"},someArray=[1.0,2.3,1.5])

#You can also use Struct literals within another Struct literal
Person person_2 = Person( name="Bob", age=45, samples=[Sample( type="Oral", sequencing_info="WES", fastq=fastq_3 )] )

```


### Struct Member Access

In order to access members within a struct, use member access notation; ie `myStruct.name`. If the underlying member is a complex type which supports member access,
Expand Down