mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Add isNotEmpty, isNotBlank methods (#1396)
Adds convenience methods `isNotEmpty` and `isNotBlank`. This borrows the same methods from Kotlin. This helps users write more fluent constraints, for example, `foo.isNotEmpty.implies(bar)`. Adds: * List#isNotEmpty * Map#isNotEmpty * Set#isNotEmpty * Mapping#isNotEmpty * Listing#isNotEmpty * String#isNotEmpty * String#isNotBlank
This commit is contained in:
@@ -181,4 +181,8 @@ public final class VmMapping extends VmListingOrMapping {
|
||||
cachedLength = count.get();
|
||||
return cachedLength;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return getLength() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -56,6 +56,13 @@ public final class ListNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isNotEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(VmList self) {
|
||||
return !self.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class lastIndex extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected long eval(VmList self) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -46,6 +46,13 @@ public final class ListingNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isNotEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(VmListing self) {
|
||||
return !self.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class lastIndex extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected long eval(VmListing self) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -52,6 +52,13 @@ public final class MapNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isNotEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(VmMap self) {
|
||||
return !self.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class keys extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected VmSet eval(VmMap self) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.pkl.core.stdlib.base;
|
||||
|
||||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
||||
import com.oracle.truffle.api.dsl.Specialization;
|
||||
import com.oracle.truffle.api.nodes.IndirectCallNode;
|
||||
import org.pkl.core.ast.lambda.ApplyVmFunction1Node;
|
||||
@@ -28,7 +27,6 @@ import org.pkl.core.stdlib.ExternalMethod0Node;
|
||||
import org.pkl.core.stdlib.ExternalMethod1Node;
|
||||
import org.pkl.core.stdlib.ExternalMethod2Node;
|
||||
import org.pkl.core.stdlib.ExternalPropertyNode;
|
||||
import org.pkl.core.util.EconomicMaps;
|
||||
import org.pkl.core.util.MutableBoolean;
|
||||
import org.pkl.core.util.MutableReference;
|
||||
|
||||
@@ -37,15 +35,15 @@ public final class MappingNodes {
|
||||
|
||||
public abstract static class isEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
@TruffleBoundary
|
||||
protected boolean eval(VmMapping self) {
|
||||
for (VmObjectLike curr = self; curr != null; curr = curr.getParent()) {
|
||||
var cursor = EconomicMaps.getEntries(curr.getMembers());
|
||||
while (cursor.advance()) {
|
||||
if (!(cursor.getKey() instanceof Identifier)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return self.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isNotEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(VmMapping self) {
|
||||
return !self.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -46,6 +46,13 @@ public final class SetNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isNotEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(VmSet self) {
|
||||
return !self.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(VmSet self) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -131,6 +131,13 @@ public final class StringNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isNotEmpty extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(String self) {
|
||||
return !self.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isBlank extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(String self) {
|
||||
@@ -138,6 +145,13 @@ public final class StringNodes {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isNotBlank extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
protected boolean eval(String self) {
|
||||
return !StringUtils.isBlank(self);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class isRegex extends ExternalPropertyNode {
|
||||
@Specialization
|
||||
@TruffleBoundary
|
||||
|
||||
@@ -12,6 +12,11 @@ facts {
|
||||
!list1.isEmpty
|
||||
}
|
||||
|
||||
["isNotEmpty"] {
|
||||
!List().isNotEmpty
|
||||
list1.isNotEmpty
|
||||
}
|
||||
|
||||
["every()"] {
|
||||
list1.every((x) -> x <= 3)
|
||||
!list1.every((x) -> x > 2)
|
||||
|
||||
@@ -42,7 +42,16 @@ facts {
|
||||
!altered.isEmpty
|
||||
!computedIndex.isEmpty
|
||||
}
|
||||
|
||||
|
||||
["isNotEmpty"] {
|
||||
!empty.isNotEmpty
|
||||
!empty2.isNotEmpty
|
||||
base.isNotEmpty
|
||||
derived.isNotEmpty
|
||||
altered.isNotEmpty
|
||||
computedIndex.isNotEmpty
|
||||
}
|
||||
|
||||
["lastIndex"] {
|
||||
empty.lastIndex == -1
|
||||
empty2.lastIndex == -1
|
||||
|
||||
@@ -8,6 +8,11 @@ facts {
|
||||
!map1.isEmpty
|
||||
}
|
||||
|
||||
["isNotEmpty"] {
|
||||
!Map().isNotEmpty
|
||||
map1.isNotEmpty
|
||||
}
|
||||
|
||||
["containsKey()"] {
|
||||
map1.containsKey("two")
|
||||
!Map().containsKey("two")
|
||||
|
||||
@@ -48,6 +48,13 @@ facts {
|
||||
empty2.isEmpty
|
||||
}
|
||||
|
||||
["isNotEmpty"] {
|
||||
base.isNotEmpty
|
||||
derived.isNotEmpty
|
||||
!empty.isNotEmpty
|
||||
!empty2.isNotEmpty
|
||||
}
|
||||
|
||||
["containsKey()"] {
|
||||
base.containsKey("Pigeon")
|
||||
base.containsKey("Parrot")
|
||||
|
||||
@@ -12,6 +12,11 @@ facts {
|
||||
!set1.isEmpty
|
||||
}
|
||||
|
||||
["isNotEmpty"] {
|
||||
!Set().isNotEmpty
|
||||
set1.isNotEmpty
|
||||
}
|
||||
|
||||
["every()"] {
|
||||
set1.every((x) -> x <= 3)
|
||||
!set1.every((x) -> x > 2)
|
||||
|
||||
@@ -11,6 +11,11 @@ facts {
|
||||
!str1.isEmpty
|
||||
}
|
||||
|
||||
["isNotEmpty"] {
|
||||
!"".isNotEmpty
|
||||
str1.isNotEmpty
|
||||
}
|
||||
|
||||
["isBlank"] {
|
||||
"".isBlank
|
||||
" ".isBlank
|
||||
@@ -18,6 +23,13 @@ facts {
|
||||
!str1.isBlank
|
||||
}
|
||||
|
||||
["isNotBlank"] {
|
||||
!"".isNotBlank
|
||||
!" ".isNotBlank
|
||||
!"\t\n\r".isNotBlank
|
||||
str1.isNotBlank
|
||||
}
|
||||
|
||||
["isBase64"] {
|
||||
"".isBase64
|
||||
"AQIDBA==".isBase64
|
||||
|
||||
@@ -3,6 +3,10 @@ facts {
|
||||
true
|
||||
true
|
||||
}
|
||||
["isNotEmpty"] {
|
||||
true
|
||||
true
|
||||
}
|
||||
["every()"] {
|
||||
true
|
||||
true
|
||||
|
||||
@@ -7,6 +7,14 @@ facts {
|
||||
true
|
||||
true
|
||||
}
|
||||
["isNotEmpty"] {
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
}
|
||||
["lastIndex"] {
|
||||
true
|
||||
true
|
||||
|
||||
@@ -3,6 +3,10 @@ facts {
|
||||
true
|
||||
true
|
||||
}
|
||||
["isNotEmpty"] {
|
||||
true
|
||||
true
|
||||
}
|
||||
["containsKey()"] {
|
||||
true
|
||||
true
|
||||
|
||||
@@ -5,6 +5,12 @@ facts {
|
||||
true
|
||||
true
|
||||
}
|
||||
["isNotEmpty"] {
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
}
|
||||
["containsKey()"] {
|
||||
true
|
||||
true
|
||||
|
||||
@@ -830,6 +830,30 @@ alias {
|
||||
name = "isEmpty"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List()
|
||||
}, "isNotEmpty", new {
|
||||
location {
|
||||
line = XXXX
|
||||
column = 3
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Tells whether this string is not empty.
|
||||
|
||||
Facts:
|
||||
```
|
||||
!"".isNotEmpty
|
||||
"abc".isNotEmpty
|
||||
```
|
||||
"""
|
||||
annotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
modifiers = Set()
|
||||
name = "isNotEmpty"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
}, "isBlank", new {
|
||||
location {
|
||||
line = XXXX
|
||||
@@ -852,6 +876,32 @@ alias {
|
||||
name = "isBlank"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List()
|
||||
}, "isNotBlank", new {
|
||||
location {
|
||||
line = XXXX
|
||||
column = 3
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Tells if at least one character is not Unicode "White_Space".
|
||||
|
||||
Facts:
|
||||
```
|
||||
!"".isNotBlank
|
||||
!" ".isNotBlank
|
||||
"\\t\\n\\r".isNotBlank
|
||||
"abc".isNotBlank
|
||||
```
|
||||
"""
|
||||
annotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
modifiers = Set()
|
||||
name = "isNotBlank"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
}, "isRegex", new {
|
||||
location {
|
||||
line = XXXX
|
||||
@@ -1124,6 +1174,30 @@ alias {
|
||||
name = "isEmpty"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List()
|
||||
}, "isNotEmpty", new {
|
||||
location {
|
||||
line = XXXX
|
||||
column = 3
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Tells whether this string is not empty.
|
||||
|
||||
Facts:
|
||||
```
|
||||
!"".isNotEmpty
|
||||
"abc".isNotEmpty
|
||||
```
|
||||
"""
|
||||
annotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
modifiers = Set()
|
||||
name = "isNotEmpty"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
}, "isBlank", new {
|
||||
location {
|
||||
line = XXXX
|
||||
@@ -1146,6 +1220,32 @@ alias {
|
||||
name = "isBlank"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List()
|
||||
}, "isNotBlank", new {
|
||||
location {
|
||||
line = XXXX
|
||||
column = 3
|
||||
displayUri = "https://github.com/apple/pkl/blob/$commitId/stdlib/base.pkl#LXXXX"
|
||||
}
|
||||
docComment = """
|
||||
Tells if at least one character is not Unicode "White_Space".
|
||||
|
||||
Facts:
|
||||
```
|
||||
!"".isNotBlank
|
||||
!" ".isNotBlank
|
||||
"\\t\\n\\r".isNotBlank
|
||||
"abc".isNotBlank
|
||||
```
|
||||
"""
|
||||
annotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
modifiers = Set()
|
||||
name = "isNotBlank"
|
||||
allModifiers = Set()
|
||||
allAnnotations = List(new {
|
||||
version = "0.31.0"
|
||||
})
|
||||
}, "isRegex", new {
|
||||
location {
|
||||
line = XXXX
|
||||
|
||||
@@ -3,6 +3,10 @@ facts {
|
||||
true
|
||||
true
|
||||
}
|
||||
["isNotEmpty"] {
|
||||
true
|
||||
true
|
||||
}
|
||||
["every()"] {
|
||||
true
|
||||
true
|
||||
|
||||
@@ -3,12 +3,22 @@ facts {
|
||||
true
|
||||
true
|
||||
}
|
||||
["isNotEmpty"] {
|
||||
true
|
||||
true
|
||||
}
|
||||
["isBlank"] {
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
}
|
||||
["isNotBlank"] {
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
}
|
||||
["isBase64"] {
|
||||
true
|
||||
true
|
||||
|
||||
Reference in New Issue
Block a user