Skip to content

Commit

Permalink
TOML model cleanups (#4907)
Browse files Browse the repository at this point in the history
* TOML model cleanups

Added a few missing `@Nullable` annotations and added accessors for `Toml.Table#name`.

* Add two more overrides to `TomlIsoVisitor`
  • Loading branch information
knutwannheden authored Jan 15, 2025
1 parent 09dfe06 commit c80874b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public Toml.Document visitDocument(Toml.Document document, P p) {
return (Toml.Document) super.visitDocument(document, p);
}

@Override
public Toml.Empty visitEmpty(Toml.Empty empty, P p) {
return (Toml.Empty) super.visitEmpty(empty, p);
}

@Override
public Toml.Identifier visitIdentifier(Toml.Identifier identifier, P p) {
return (Toml.Identifier) super.visitIdentifier(identifier, p);
Expand All @@ -43,4 +48,9 @@ public Toml.KeyValue visitKeyValue(Toml.KeyValue keyValue, P p) {
public Toml.Literal visitLiteral(Toml.Literal literal, P p) {
return (Toml.Literal) super.visitLiteral(literal, p);
}

@Override
public Toml.Table visitTable(Toml.Table table, P p) {
return (Toml.Table) super.visitTable(table, p);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public Toml visitTable(Toml.Table table, PrintOutputCapture<P> p) {
p.append("}");
} else if (table.getMarkers().findFirst(ArrayTable.class).isPresent()) {
p.append("[[");
visitRightPadded(table.getName(), p);
visitRightPadded(table.getPadding().getName(), p);
p.append("]]");
visitRightPadded(table.getPadding().getValues(), "", p);
} else {
p.append("[");
visitRightPadded(table.getName(), p);
visitRightPadded(table.getPadding().getName(), p);
p.append("]");
visitRightPadded(table.getPadding().getValues(), "", p);
}
Expand Down
25 changes: 23 additions & 2 deletions rewrite-toml/src/main/java/org/openrewrite/toml/tree/Toml.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ class Document implements Toml, SourceFile {
String charsetName;

boolean charsetBomMarked;

@Nullable
Checksum checksum;

@Nullable
FileAttributes fileAttributes;

@Override
Expand All @@ -137,7 +141,8 @@ public Charset getCharset() {
}

@Override
public SourceFile withCharset(Charset charset) {
@SuppressWarnings("unchecked")
public Document withCharset(Charset charset) {
return withCharsetName(charset.name());
}

Expand Down Expand Up @@ -309,9 +314,17 @@ class Table implements TomlValue {
@With
Markers markers;

@With
@Nullable
TomlRightPadded<Toml.Identifier> name;

public Toml.@Nullable Identifier getName() {
return name != null ? name.getElement() : null;
}

public Table withName(Toml.@Nullable Identifier name) {
return getPadding().withName(TomlRightPadded.withElement(this.name, name));
}

List<TomlRightPadded<Toml>> values;

public List<Toml> getValues() {
Expand Down Expand Up @@ -353,6 +366,14 @@ public List<TomlRightPadded<Toml>> getValues() {
public Table withValues(List<TomlRightPadded<Toml>> values) {
return t.values == values ? t : new Table(t.id, t.prefix, t.markers, t.name, values);
}

public @Nullable TomlRightPadded<Toml.Identifier> getName() {
return t.name;
}

public Table withName(@Nullable TomlRightPadded<Identifier> name) {
return t.name == name ? t : new Table(t.id, t.prefix, t.markers, name, t.values);
}
}
}
}

0 comments on commit c80874b

Please sign in to comment.