Skip to content

Commit

Permalink
fix: fix documentation example code
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Aug 7, 2023
1 parent d8ee785 commit a5987a1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
9 changes: 5 additions & 4 deletions examples/csharp/documentation/FunctionsOther.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ public FunctionsOtherStack(Construct scope, string name) : base(scope, name)
// DOCS_BLOCK_END:functions-raw

// DOCS_BLOCK_START:functions-lookup
TerraformVariable v = new TerraformVariable(this, "complex_object", TerraformVariableConfig.builder()
.type("object({users: list(object({name: string}))})")
.build());
TerraformVariable v = new TerraformVariable(this, "complex_object", new TerraformVariableConfig
{
Type = "object({users: list(object({name: string}))})",
});
new TerraformOutput(this, "users", new TerraformOutputConfig
{
Value = Fn.Lookup(v.Value, "users")
});
new TerraformOutput(this, "first-user-name", new TerraformOutputConfig
{
Value = Fn.LookupNested(v.Value, new() { "users", "0", "name" })
Value = Fn.LookupNested(v.Value, new[] { "users", "0", "name" })
});
// DOCS_BLOCK_END:functions-lookup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// DOCS_BLOCK_START:functions-usage-example
import com.hashicorp.cdktf.Fn;
import com.hashicorp.cdktf.TerraformVariable;
import com.hashicorp.cdktf.TerraformVariableConfig;
import com.hashicorp.cdktf.TerraformOutput;
import com.hashicorp.cdktf.TerraformOutputConfig;
import imports.aws.data_aws_availability_zones.DataAwsAvailabilityZones;
Expand Down
8 changes: 5 additions & 3 deletions examples/python/documentation/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: MPL-2.0

from constructs import Construct
from cdktf import TerraformStack, App, TerraformVariable
from cdktf import TerraformStack, App, TerraformVariable, Token
# DOCS_BLOCK_START:functions-usage-example
from cdktf import Fn, TerraformOutput
from imports.aws.provider import AwsProvider
Expand All @@ -28,12 +28,14 @@ def __init__(self, scope: Construct, id: str):

# DOCS_BLOCK_END:functions-usage-example

# INTERNAL NOTE: Due to an JSII bug, we have to pass a default value for lookup in Python
# We can remove it, once https://github.com/aws/jsii/pull/4209 is released
# DOCS_BLOCK_START:functions-lookup
v = TerraformVariable(self, "complex-object",
type = 'object({users: list(object({name: string}))})',
)
TerraformOutput(self, 'users',
value=Fn.lookup(v.value, "users")
value=Fn.lookup(v.value, "users", "default")
)
TerraformOutput(self, 'first_user_name',
value=Fn.lookup_nested(v.value, ["users", 0, "name"])
Expand All @@ -44,7 +46,7 @@ def __init__(self, scope: Construct, id: str):
TerraformOutput(self, 'quotes',
value=Fn.raw_string('"b"')
)
TerraformOutput(self, 'users',
TerraformOutput(self, 'template',
value=Fn.raw_string('${TEMPLATE}')
)
# DOCS_BLOCK_END:functions-raw-string
Expand Down
5 changes: 3 additions & 2 deletions packages/cdktf/lib/terraform-functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
import { propertyAccess, rawString, Token } from ".";
import { asAny } from "./functions/helpers";
import { FnGenerated } from "./functions/terraform-functions.generated";

// eslint-disable-next-line jsdoc/require-jsdoc
Expand All @@ -26,7 +27,7 @@ export class Fn extends FnGenerated {
static lookup(inputMap: any, key: string, defaultValue?: any) {
// overwritten because lookup() uses a variadic argument for its optional defaultValue
if (defaultValue) return Fn._lookup(inputMap, key, [defaultValue]);
return propertyAccess(inputMap, [key]); // -> renders inputMap[key] (which is recommened if no default value is given)
return asAny(propertyAccess(inputMap, [key])); // -> renders inputMap[key] (which is recommended if no default value is given)
}

/**
Expand All @@ -36,7 +37,7 @@ export class Fn extends FnGenerated {
* @param {Array<any>} path
*/
static lookupNested(inputMap: any, path: any[]) {
return propertyAccess(inputMap, path);
return asAny(propertyAccess(inputMap, path));
}

/**
Expand Down
14 changes: 8 additions & 6 deletions website/docs/cdktf/concepts/functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class FunctionsStack extends TerraformStack {
```java
import com.hashicorp.cdktf.Fn;
import com.hashicorp.cdktf.TerraformVariable;
import com.hashicorp.cdktf.TerraformVariableConfig;
import com.hashicorp.cdktf.TerraformOutput;
import com.hashicorp.cdktf.TerraformOutputConfig;
import imports.aws.data_aws_availability_zones.DataAwsAvailabilityZones;
Expand Down Expand Up @@ -180,7 +181,7 @@ v = TerraformVariable(self, "complex-object",
type = 'object({users: list(object({name: string}))})',
)
TerraformOutput(self, 'users',
value=Fn.lookup(v.value, "users")
value=Fn.lookup(v.value, "users", "default")
)
TerraformOutput(self, 'first_user_name',
value=Fn.lookup_nested(v.value, ["users", 0, "name"])
Expand All @@ -200,16 +201,17 @@ new TerraformOutput(this, "first-user-name", TerraformOutputConfig.builder()
```

```csharp
TerraformVariable v = new TerraformVariable(this, "complex_object", TerraformVariableConfig.builder()
.type("object({users: list(object({name: string}))})")
.build());
TerraformVariable v = new TerraformVariable(this, "complex_object", new TerraformVariableConfig
{
Type = "object({users: list(object({name: string}))})",
});
new TerraformOutput(this, "users", new TerraformOutputConfig
{
Value = Fn.Lookup(v.Value, "users")
});
new TerraformOutput(this, "first-user-name", new TerraformOutputConfig
{
Value = Fn.LookupNested(v.Value, new() { "users", "0", "name" })
Value = Fn.LookupNested(v.Value, new[] { "users", "0", "name" })
});
```

Expand Down Expand Up @@ -248,7 +250,7 @@ new TerraformOutput(this, "template", {
TerraformOutput(self, 'quotes',
value=Fn.raw_string('"b"')
)
TerraformOutput(self, 'users',
TerraformOutput(self, 'template',
value=Fn.raw_string('${TEMPLATE}')
)
```
Expand Down

0 comments on commit a5987a1

Please sign in to comment.