MCPcopy
hub / github.com/bazelbuild/bazel / Struct

Class Struct

src/test/java/net/starlark/java/eval/ScriptTest.java:357–392  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

355
356 // A trivial struct-like class with Starlark fields defined by a map.
357 private static class Struct implements StarlarkValue, Structure {
358 final Map<String, Object> fields;
359
360 Struct(Map<String, Object> fields) {
361 this.fields = fields;
362 }
363
364 @Override
365 public ImmutableList<String> getFieldNames() {
366 return ImmutableList.copyOf(fields.keySet());
367 }
368
369 @Override
370 public Object getValue(String name) {
371 return fields.get(name);
372 }
373
374 @Override
375 public String getErrorMessageForUnknownField(String name) {
376 return null;
377 }
378
379 @Override
380 public void repr(Printer p) {
381 // This repr function prints only the fields.
382 // Any methods are still accessible through dir/getattr/hasattr.
383 p.append(Starlark.type(this));
384 p.append("(");
385 String sep = "";
386 for (Map.Entry<String, Object> e : fields.entrySet()) {
387 p.append(sep).append(e.getKey()).append(" = ").repr(e.getValue());
388 sep = ", ";
389 }
390 p.append(")");
391 }
392 }
393
394 @StarlarkBuiltin(name = "struct")
395 private static class ImmutableStruct extends Struct {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected