Skip to content

Commit

Permalink
Add entries to Mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
holzensp committed Oct 25, 2024
1 parent 6fc7879 commit a7e8dfc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkl-core/src/main/java/org/pkl/core/stdlib/base/MappingNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ protected VmList eval(VmMapping self) {
}
}

public abstract static class entries extends ExternalPropertyNode {
@Specialization
protected VmList eval(VmMapping self) {
var builder = VmList.EMPTY.builder();
self.forceAndIterateMemberValues(
(key, member, value) -> {
builder.add(new VmPair(key, value));
return true;
});
return builder.build();
}
}

public abstract static class containsKey extends ExternalMethod1Node {
@Specialization
protected boolean eval(VmMapping self, Object key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ facts {
base.values == base.toMap().values
derived.values == derived.toMap().values
}

["entries"] {
empty.entries == List()
base.entries == base.toMap().entries
derived.entries == derived.toMap().entries
}
}

examples {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ facts {
true
true
}
["entries"] {
true
true
true
}
}
examples {
["getOrNull()"] {
Expand Down
3 changes: 3 additions & 0 deletions stdlib/base.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,9 @@ class Mapping<out Key, out Value> extends Object {

/// The values contained in this mapping.
external values: List<Value>

/// The entries contained in this mapping.
external entries: List<Pair<Key, Value>>

/// Tells if this mapping contains [key].
external function containsKey(key: Any): Boolean
Expand Down

0 comments on commit a7e8dfc

Please sign in to comment.