Skip to main content

Inflight Magazine no. 5

· 7 min read
Danna Cahana
Mark McCulloh

The 5th issue of the Wing Inflight Magazine.

Hey folks!

We’re back with another issue of the Wing Inflight Magazine and we are excited to share some updates about winglang.

Variadic arguments

One of those language features you forgot about until you need it, and then you really really need it. Wing now supports variadic arguments for function type declarations.

let sum = (...sums: Array<num>): num => {
let var total = 0;
for n in nums {
total = total + n;
}
return total;
};

assert(sum(1, 2, 3) == 6);

This also means that variadic arguments are supported when importing APIs from JSII libraries.

❤️ Marcio Cruz, Chris Rybicki, and Lance Janssen ❤️

Added set() API to MutArray

The MutArray is now even mutier!

New APIs have been added to MutArray, Winglang's mutable array object. The set() API allows you to change an array value at a particular index, and insert() allows inserting an element at a specific (0-based index) position.

The new set(index: num, value: T) API sets a value at the given index of an array:

let letters = MutArray<str>["A", "B", "D", "D"];
letters.set(2, "C");

assert(letters == ["A", "B", "C", "D"];

The new insert(index: num, value: T) API inserts a new element at the given index of an array:

let letters = MutArray<str>["A", "B", "D"];
letters.insert(2, "C");

assert(letters == ["A", "B", "C", "D"];

❤️ Gary Sassano, Ananthu C V ❤️

Schema when parsing JSON into structs

One of the most sought-after features since the beginning of Wing, we now supports runtime checking for JSON objects.

A MyStruct.fromJson() and MyStruct.parseJson() static methods are automatically generated for structs and perform schema validation based on the struct declaration. This method allows you to convert input data into a struct while doing runtime validation. If the input doesn't match the expected schema, a proper error message is generated.

struct Person {
name: str;
age: num;
}

let j = { name: "Old McOld", age: 87 };
let realPerson = Person.fromJson(j);

With schema validation:

let badPerson = Person.fromJson({ name: "Stringy Fella", age: "87?" });

// ERROR: unable to parse Person:
// instance.age is not of a type(s) number

❤️ Hasan Abu-Rayyan ❤️

Implicit casting from Json to structs

A new compiler feature allows for automatic "inference" or casting of JSON objects to structs, if the types structurally match. This makes it easier to use JSON objects as structs and reduces the amount of type specification code you have to write.

Before: look how gross and stinky this is:

bring "@cdktf/provider-kubernetes" as k8s;

let ns = new k8s.namespace.Namespace(
metadata: k8s.namespace.NamespaceMetadata { name: "nginx" }
);

let deployment = new k8s.deployment.Deployment(
metadata: k8s.deployment.DeploymentMetadata {
name: "nginx",
namespace: ns.metadata.name,
},
spec: k8s.deployment.DeploymentSpec {
template: k8s.deployment.DeploymentSpecTemplate {
metadata: k8s.deployment.DeploymentSpecTemplateMetadata { labels: { app: "nginx" } },
spec: k8s.deployment.DeploymentSpecTemplateSpec {
container: [
k8s.deployment.DeploymentSpecTemplateSpecContainer {
name: "nginx-container",
image: "nginx",
port: k8s.deployment.DeploymentSpecTemplateSpecContainerPort { container_port: 80 },
}
]
}
}
}
);

After: oh hell yeah brother! And it is still strongly typed:

bring "@cdktf/provider-kubernetes" as k8s;

let ns = new k8s.namespace.Namespace(
metadata: { name: "nginx" }
);

let deployment = new k8s.deployment.Deployment(
metadata: {
name: "nginx",
namespace: ns.metadata.name,
},
spec: {
template: {
metadata: { labels: { app: "nginx" } },
spec: {
container: [
{
name: "nginx-container",
image: "nginx",
port: { container_port: 80 },
}
]
}
}
}
);

❤️ Mark McCulloh ❤️

Added addFile() API to Bucket

Super common use-case, now *super easy! A new API called addFile() has been added to the bucket object. This is a preflight API* which can be used to populate files into a bucket during deployment.

bring cloud;

let b = new cloud.Bucket();
b.addFile("path/in/bucket/file.txt", "local/path/file.txt");

This will copy the file from local/path/file.txt to the bucket during deployment.

❤️ Akhil Narayandas (first contribution ❤️)

Packaging Wing Libraries

With the new subcommand wing pack, you can now package up your Wing project as a library and publish it to npm. It produces an npm-compatible tarball that can be published and installed.

❤️ Chris Rybicki ❤️

Directory modules

Another addition from Chris Rybicki is the ability to bring in and access entire directories instead of just individual files. Now, within these directories, users can access types, subdirectories, and their contents. This enhancement not only aids in organizing projects within Winglang but also serves as a foundational step towards supporting Wing libraries.

❤️ Chris Rybicki ❤️

fs Namespace

Wing now has a comprehensive file system module. It covers standard file operations but also integrates unique functions for reading and writing JSON and YAML. An awesome feature is the normalization of file paths: by default, all returned paths from these operations will use forward slashes, standardizing file paths across platforms. We believe this approach will provide consistency and ease for developers working in mixed platform environments.

❤️ Jade Zheng ❤️

Upcoming Events

🔥 Live Workshop - December 6th @4:30 - 6pm IST Get hands-on with Eyal Keren as he walks us through the process of constructing a full-fledged React.js application using Winglang. For more information and to register, click here.

The Wingly Update

The Wingly Update is our biweekly stream where we share the latest developments of the project, chat with folks from the cloud industry, geek out and celebrate the beauty of the cloud.

If you haven't been able to catch our show, don't fret! You can find the complete stack of all our episodes here.

Here are a few segments from the show we thought might be interesting to check out:

  • How to maintain state in a simulated Wing resource?: In this clip from The Wingly Update #21, Elad walks through storign state when building simulated resources in Winglang.
  • Winglang CHANGELOG Segment: In this clip from The Wingly Update #21, Mark walks us through the latest updates in Winglang, such as access modifiers for types, filtering tests, running multiple tests simultaneously with ‘awscdk’, testing and ‘inflight’ in Azure, and more!
  • How to build and test container-based apps in Wing?: Containers are finally taking flight! In this video from The Wingly Update #20, Elad and Eyal are playing around with containers in Wing. They show how local simulation works with Docker and how apps can be seamlessly deployed to Kubernetes through Helm.
  • Winglang Meets Darko Mesaroš from AWS: In this clip from The Wingly Update #19, Darko Mesaroš - an AWS Developer Advocate, joins the show. Tune in to hear about Darko’s background as a developer advocate, his hobby of collecting old computers and his ideas and suggestions for Wing.

Summary

And that’s a wrap for this edition!

If you’re not already, make sure to keep following the latest updates and changes in our repo.

You'll find us regularly mingling on Slack, so feel free to introduce yourself, and if you haven't yet, give winglang.io a visit and take Wing out for a spin.

See you in our next flight! (sorry, but we just can't help it with the puns...)