mirror of
https://github.com/apple/pkl.git
synced 2026-04-11 03:06:55 +02: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
|
||||
|
||||
Reference in New Issue
Block a user