Skip to content

Commit ff3eb4e

Browse files
committed
fix(lazy): implement instanceof properly
Fixes: cdk8s-team/cdk8s#2164 Signed-off-by: Nikolai Prokoschenko <nikolai.prokoschenko@kurzdigital.com>
1 parent 06b0292 commit ff3eb4e

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

docs/java.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,6 +3666,7 @@ public java.lang.Object produce()
36663666
| **Name** | **Description** |
36673667
| --- | --- |
36683668
| <code><a href="#cdk8s.Lazy.any">any</a></code> | *No description.* |
3669+
| <code><a href="#cdk8s.Lazy.isLazy">isLazy</a></code> | Checks if an object is a Lazy instance. |
36693670

36703671
---
36713672

@@ -3683,6 +3684,24 @@ Lazy.any(IAnyProducer producer)
36833684

36843685
---
36853686

3687+
##### `isLazy` <a name="isLazy" id="cdk8s.Lazy.isLazy"></a>
3688+
3689+
```java
3690+
import org.cdk8s.Lazy;
3691+
3692+
Lazy.isLazy(java.lang.Object obj)
3693+
```
3694+
3695+
Checks if an object is a Lazy instance.
3696+
3697+
###### `obj`<sup>Required</sup> <a name="obj" id="cdk8s.Lazy.isLazy.parameter.obj"></a>
3698+
3699+
- *Type:* java.lang.Object
3700+
3701+
The object to check.
3702+
3703+
---
3704+
36863705

36873706

36883707
### LazyResolver <a name="LazyResolver" id="cdk8s.LazyResolver"></a>

docs/python.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,6 +3881,7 @@ def produce() -> typing.Any
38813881
| **Name** | **Description** |
38823882
| --- | --- |
38833883
| <code><a href="#cdk8s.Lazy.any">any</a></code> | *No description.* |
3884+
| <code><a href="#cdk8s.Lazy.isLazy">is_lazy</a></code> | Checks if an object is a Lazy instance. |
38843885

38853886
---
38863887

@@ -3900,6 +3901,26 @@ cdk8s.Lazy.any(
39003901

39013902
---
39023903

3904+
##### `is_lazy` <a name="is_lazy" id="cdk8s.Lazy.isLazy"></a>
3905+
3906+
```python
3907+
import cdk8s
3908+
3909+
cdk8s.Lazy.is_lazy(
3910+
obj: typing.Any
3911+
)
3912+
```
3913+
3914+
Checks if an object is a Lazy instance.
3915+
3916+
###### `obj`<sup>Required</sup> <a name="obj" id="cdk8s.Lazy.isLazy.parameter.obj"></a>
3917+
3918+
- *Type:* typing.Any
3919+
3920+
The object to check.
3921+
3922+
---
3923+
39033924

39043925

39053926
### LazyResolver <a name="LazyResolver" id="cdk8s.LazyResolver"></a>

docs/typescript.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,7 @@ public produce(): any
31833183
| **Name** | **Description** |
31843184
| --- | --- |
31853185
| <code><a href="#cdk8s.Lazy.any">any</a></code> | *No description.* |
3186+
| <code><a href="#cdk8s.Lazy.isLazy">isLazy</a></code> | Checks if an object is a Lazy instance. |
31863187

31873188
---
31883189

@@ -3200,6 +3201,24 @@ Lazy.any(producer: IAnyProducer)
32003201

32013202
---
32023203

3204+
##### `isLazy` <a name="isLazy" id="cdk8s.Lazy.isLazy"></a>
3205+
3206+
```typescript
3207+
import { Lazy } from 'cdk8s'
3208+
3209+
Lazy.isLazy(obj: any)
3210+
```
3211+
3212+
Checks if an object is a Lazy instance.
3213+
3214+
###### `obj`<sup>Required</sup> <a name="obj" id="cdk8s.Lazy.isLazy.parameter.obj"></a>
3215+
3216+
- *Type:* any
3217+
3218+
The object to check.
3219+
3220+
---
3221+
32033222

32043223

32053224
### LazyResolver <a name="LazyResolver" id="cdk8s.LazyResolver"></a>

src/lazy.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
const LAZY_SYMBOL = Symbol.for("cdk8s.Lazy");
2+
13
export class Lazy {
24
public static any(producer: IAnyProducer): any {
35
return new Lazy(producer) as unknown as any;
46
}
57

6-
private constructor(private readonly producer: IAnyProducer) {}
8+
/**
9+
* Checks if an object is a Lazy instance
10+
* @param obj The object to check
11+
*/
12+
static isLazy(obj: any): boolean {
13+
return obj !== null && typeof obj === "object" && LAZY_SYMBOL in obj;
14+
}
15+
16+
/**
17+
* Implements `instanceof Lazy` using the more reliable `Lazy.isLazy` static method
18+
*
19+
* @param obj The object to check
20+
* @internal
21+
*/
22+
public static [Symbol.hasInstance](obj: any): boolean {
23+
return Lazy.isLazy(obj);
24+
}
25+
26+
private constructor(private readonly producer: IAnyProducer) {
27+
Object.defineProperty(this, LAZY_SYMBOL, { value: true });
28+
}
729

830
public produce(): any {
931
return this.producer.produce();
@@ -13,5 +35,3 @@ export class Lazy {
1335
export interface IAnyProducer {
1436
produce(): any;
1537
}
16-
17-

0 commit comments

Comments
 (0)