Skip to content

Add Record and Data classes#2232

Merged
arnaudroques merged 5 commits into
plantuml:masterfrom
Intybyte:feature/data-record-classes
Jun 14, 2025
Merged

Add Record and Data classes#2232
arnaudroques merged 5 commits into
plantuml:masterfrom
Intybyte:feature/data-record-classes

Conversation

@Intybyte
Copy link
Copy Markdown
Contributor

I used #1028 and #1056 as a base, Data classes are a mutable set of data, whereas record are immutable set of data, many programming language implemented the latter (Java, C#, etc.), the former is less used but still present (POJOs and kotlin's data classes).

Let me know if there are any needed change, or I missed something

@arnaudroques
Copy link
Copy Markdown
Contributor

Thanks, this sounds like a good idea.

However, I would have used "dataclass" instead of "data class" (for consistency with other type)

Which means change the regex data[%s]+class to dataclass

Does it make sense ?

Comment thread src/main/java/net/sourceforge/plantuml/abel/LeafType.java Outdated
@arnaudroques
Copy link
Copy Markdown
Contributor

I may be wrong, but shouldn't we have dataclass here:

"GENDER", "(?:(class|object|interface|enum|annotation|data|record|abstract|[%pLN_.]+|[%g][^%g]+[%g]|\\<\\<.*\\>\\>)[%s]+)*?"), //

@arnaudroques arnaudroques merged commit b8bb8a7 into plantuml:master Jun 14, 2025
10 checks passed
@arnaudroques
Copy link
Copy Markdown
Contributor

Many thanks for your contribution!

If possible, could you also add some basic examples to https://github.com/plantuml/pdiff ? This tool is used to manage non-regression tests.

If you're unsure how to do this, feel free to share your examples here in the thread, we'll be happy to add them to pdiff ourselves.

PS:
I think that this is not needed anymore in LeafType.java

		if (type.startsWith("DATA"))
			return LeafType.DATACLASS;

@Intybyte
Copy link
Copy Markdown
Contributor Author

Intybyte commented Jun 14, 2025

Yeah without a reference I have no idea how to add my references there, however I can show some examples here for record and dataclass in java

These are the java classes:

The java code for the record

public record Key(String namespace, String key) {
    public static final Pattern NAMESPACED_KEY_PATTERN = Pattern.compile("^[a-z0-9._-]+:[a-z0-9._-]+$");

    public Key(String namespace, String key) {
        this.namespace = namespace.toLowerCase(Locale.ROOT);
        this.key = key.toLowerCase(Locale.ROOT);

        if (!NAMESPACED_KEY_PATTERN.matcher(this.full()).matches()) {
            throw new IllegalArgumentException("Invalid NS key: " + this.full());
        }
    }

    public String full() {
        return namespace + ":" + key;
    }

    @Override
    public String toString() {
        return "Key[" +
                "namespace=" + namespace + ", " +
                "key=" + key + ']';
    }
}

To PlantUML

@startuml
record Key {
  - namespace : String
  - key : String
  + {static} NAMESPACED_KEY_PATTERN : Pattern
  + Key(namespace : String, key : String)
  + full() : String
  + toString() : String
}

@enduml

A java POJO (dataclass example)

// lombok data annotation generates setters and getters
@Data public class AngleData {
    private double horizontalAngle;
    private double verticalAngle;
    private double additionalHorizontalAngle;
    private double additionalVerticalAngle;
}

To PlantUML

dataclass AngleData <<lombok>> {
  - aimingPitch : double
  - aimingYaw : double
  - aimingFinished : boolean
  - lastAimed : long
  - timestampAimingMode : long
}

@arnaudroques
Copy link
Copy Markdown
Contributor

For the record, done here plantuml/pdiff@f2d71d2

arnaudroques added a commit that referenced this pull request Jun 15, 2025
@The-Lum
Copy link
Copy Markdown
Collaborator

The-Lum commented Jun 16, 2025

Hi @Intybyte, @arnaudroques

To anticipate new request...

  • Do you have any other keywords evolutions in mind for class diagrams?

Hints:

Ref.:

Regards,
Th.

@Intybyte
Copy link
Copy Markdown
Contributor Author

Hi @Intybyte, @arnaudroques

To anticipate new request...

* Do you have any other keywords evolutions in mind for class diagrams?

Hints:

* [Feature Request: abstract entity #1438](https://github.com/plantuml/plantuml/issues/1438)

* module _(and [Module System](https://openjdk.org/jeps/261))_

* ...

Ref.:

* https://en.wikipedia.org/wiki/List_of_Java_keywords

Regards, Th.

I think the module keyword might be useful in the future for adding another layer of abstraction over packages

Also worth considering are sealed classes, though they’d require special handling to define permitted subclasses

@The-Lum
Copy link
Copy Markdown
Collaborator

The-Lum commented Jun 22, 2025

[Just for the record]

I found (also) this request:

Regards,
Th.

The-Lum added a commit that referenced this pull request Sep 13, 2025
arnaudroques pushed a commit that referenced this pull request Sep 13, 2025
…ywords] (#2336)

* 🐛 add `floating note` on Language Descriptor 

Fix /issues/2334

* ✨ add Network Diagram keywords on Language Descriptor

* ⚗️ add minimum Timing Diagram keywords on Language Descriptor

* 🐛 fix Activity Diagram keywords on Language Descriptor

* 🔥 suppress duplicate `bold` on Language Descriptor

* ✨ add `dataclass` and `record` on Language Descriptor 

Ref.:
- #2232

* 🐛 fix JSON/YAML keywords on Language Descriptor

* 🚧 add Preproc keywords on Language Descriptor

To be conform to:
- https://github.com/plantuml/plantuml/blob/7d3a9ce1abdba648497c2bb7e73c8b40d415631a/src/main/java/net/sourceforge/plantuml/text/TLineType.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants