Comments (4)
@holzensp Another lovely feature that I'm using a lot now.
@HT154 You need to write that up in the documentation somewhere. I can see how each of the options could be used as a feature and your description is great.
from pkl.
This comes down to what is being amended. There's actually yet another option here that looks like this:
[8088] {
port = 8088
targetPort = port
}
Here's what each of these idioms means in (more) plain language:
- direct amend (my above example) - amend the parent value
= new {
- amend the default value as defined in the Mapping/Listing/class= new Service.ServicePort {
- creates a full newService.ServicePort
(amending only that class's default values)
It's a little easier to see it in action, here's a more complete example:
class MyThing {
favoriteColors: Listing<String> = new {
"blue"
}
}
directAmend: MyThing = new { // this `new` makes a MyThing and amends the class's defaults
favoriteColors { // this amend adds to the parent (the new MyThing) and its defaults
"red"
}
}
directAmendAgain: MyThing = (directAmend) { // this amends `directAmend`
favoriteColors { // this amend adds to the parent (`directAmend`) and its value
"yellow"
}
}
defaultAmend: MyThing = (directAmend) {
favoriteColors = new { // this amends the default value of `MyThing`'s `favoriteColors`
"yellow"
}
}
noAmend: MyThing = (directAmend) {
favoriteColors = new Listing<String> { // this amends the default value of `Listing<String> ` (which is empty)
"yellow"
}
}
This evaluates to:
directAmend {
favoriteColors { // the class's defaults, adding "red"
"blue"
"red"
}
}
directAmendAgain {
favoriteColors { // directAmend's value, adding "yellow"
"blue"
"red"
"yellow"
}
}
defaultAmend {
favoriteColors { // the class's defaults, adding "yellow"
"blue"
"yellow"
}
}
noAmend {
favoriteColors { // an empty listing, adding "yellow"
"yellow"
}
}
Using default
in a Mapping or Listing works similarly to the class default value I used in this example:
someThings: Listing<MyThing> = new {
default {
favoriteColors { // this amends the default value of `MyThing`'s `favoriteColors`
"green"
}
}
new { // this amends `someThings`'s `default`
favoriteColors {
"purple"
}
}
new MyThing { // this `new` makes a MyThing and amends the class's defaults (ignoring the Listing's `default`)
favoriteColors {
"purple"
}
}
new { // this amends `someThings`'s `default`
favoriteColors = new { // but this amend's the class's default value (ignoring the Listing's `default`)
"purple"
}
}
new MyThing { // this `new` makes a MyThing and amends the class's defaults (ignoring the Listing's `default`)
favoriteColors = new { // and this amend's the class's default value
"purple"
}
}
new { // this amends `someThings`'s `default`
favoriteColors = new Listing<String> { // but this creates a new empty Listing and amends it
"purple"
}
}
new MyThing { // this `new` makes a MyThing and amends the class's defaults (ignoring the Listing's `default`)
favoriteColors = new Listing<String> { // but this creates a new empty Listing and amends it
"purple"
}
}
}
This evaluates to:
someThings {
new {
favoriteColors {
"blue"
"green"
"purple"
}
}
new {
favoriteColors {
"blue"
"purple"
}
}
new {
favoriteColors {
"blue"
"purple"
}
}
new {
favoriteColors {
"blue"
"purple"
}
}
new {
favoriteColors {
"purple"
}
}
new {
favoriteColors {
"purple"
}
}
}
from pkl.
Totally what @HT154 said. Also, looking at your code, I'd suggest substituting lines 50-65 with
default { _port ->
appProtocol = "HTTP"
name = appProtocol.toLowerCase()
protocol = "TCP"
port = _port
targetPort = port
}
[8086] {}
[8081] {
name = "debug"
}
- You don't need to repeat all the
targetPort = port
; that's unchanged from the default - The type of
Mapping<K,V>.default
is(K) -> V
; it's a function, so you can use the value of the key to express the default value of the entry.
Also, the property-by-property copy (lines 28-34) confuses me. Do you want everything as in sp
, except the nodePort
?
from pkl.
I'd love to get this into the docs! I'll defer to @holzensp and crew on where the best place to put it is. Maybe a new section under the language reference's Advanced Topics?
from pkl.
Related Issues (20)
- Bug: fatal error when chart location is invalid HOT 7
- Automatic formatting of Pkl files HOT 1
- Stack overflow when parameter name is same as object property name HOT 2
- Type constraints on `typealias`es are evaluated eagerly HOT 3
- Question: Submit to CNCF HOT 3
- `gw buildNative` runs aarch64 tasks on amd64 HOT 1
- Allow overriding Java/Kotlin package name in codegen
- Rendering lists in XML may produce unexpected results HOT 1
- Type constraints are sometimes not enforced. HOT 2
- Unable to create test for non-empty constraint on listing HOT 2
- Add SLSA provenance to your releases
- Disable "Allow merge commits" and "Allow rebase merging" in GitHub UI HOT 3
- Non-constant globbed reads return wrong results
- Reads with same glob pattern return same result even if located in different modules/directories
- better source listing for IntelliJ to include line numbers? HOT 2
- Provide an ability to depend on a local project module from a non-project module
- Feature Request: Add `hcl` format
- [Feature Request] Type parameters for Classes
- Unexpected caching with pkl-go and Evaluator.EvaluateOutputText HOT 1
- Output path placeholders don't expose `ValueRenderer.extension`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pkl.