Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ build/

### VS Code ###
.vscode/*
!.vscode/settings.json

### MacOS ###
.DS_Store
Binary file removed .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
19 changes: 18 additions & 1 deletion .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

25 changes: 23 additions & 2 deletions annotation/src/main/java/online/sharedtype/SharedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@

/**
* Type literal to be emitted for date/time types. How a java type is considered a date/time type is defined by global properties.
*
* @return any literal, e.g. "string", "Date". When empty, fallback to global default.
*/
String goTargetDatetimeTypeLiteral() default "";

/**
* Format of enum in Go.
*
* @return "const" or "struct". If empty, fallback to global default.
*/
String goEnumFormat() default "";
Expand Down Expand Up @@ -325,22 +327,37 @@
}

/**
* Add any tag literals to a field. E.g.
* Add any tag literals to a field. E.g. below will add "#[serde(skip)]" tag literal to the emitted Rust struct field.
* <pre>
* {@code
* @SharedType.TagLiterals(tags = "#[serde(skip)]", targets = RUST)
* private final Object ignoredField;
* }
* </pre>
* It's treated as plain string, thus can also be used to emit comments or documentation.
* It's treated as plain text, thus can also be used to emit comments, documentation, or Golang struct tags.
*/
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@Repeatable(TagLiterals.class)
@interface TagLiteral {
/**
* Tag literals will be emitted for the field.
*
* @see #position()
*/
String[] tags();
/** If empty, fallback to globally enabled targets. */
TargetType[] targets() default {};

/**
* <ul>
* <li>{@link TagPosition#NEWLINE_ABOVE} - emitted above the field. Every tag content will be in a new line.</li>
* <li>{@link TagPosition#INLINE_AFTER} - emitted after the field at the same line. Tags will be separated by space.
* This can be used to generate Golang struct tags as well. <b>Note:</b> this will override default behavior.
* By default, Golang struct tag contains minimal json tags, e.g. `json:"fieldName,omitempty"`, "omitempty" is added when the field is optional.</li>
* </ul>
*/
TagPosition position() default TagPosition.NEWLINE_ABOVE;
}

enum ComponentType {
Expand Down Expand Up @@ -385,4 +402,8 @@ enum OptionalBool {
enum TargetType {
TYPESCRIPT, GO, RUST
}

enum TagPosition {
NEWLINE_ABOVE, INLINE_AFTER,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public abstract class AbstractComponentInfo implements ComponentInfo {
private transient final Element element;
private final String name;
@Setter @Builder.Default
private Map<SharedType.TargetType, List<String>> tagLiterals = Collections.emptyMap();
private Map<SharedType.TargetType, List<TagLiteralContainer>> tagLiterals = Collections.emptyMap();

@Override
public final String name() {
return name;
}

@Override
public final List<String> getTagLiterals(SharedType.TargetType targetType) {
public final List<TagLiteralContainer> getTagLiterals(SharedType.TargetType targetType) {
return tagLiterals.getOrDefault(targetType, Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
public interface ComponentInfo extends Serializable {
boolean resolved();
String name();
List<String> getTagLiterals(SharedType.TargetType targetType);
List<TagLiteralContainer> getTagLiterals(SharedType.TargetType targetType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package online.sharedtype.processor.domain.component;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import online.sharedtype.SharedType;

import java.io.Serializable;
import java.util.List;

@ToString
@EqualsAndHashCode
@Getter
@RequiredArgsConstructor
public final class TagLiteralContainer implements Serializable {
private static final long serialVersionUID = 916098025299922397L;
private final List<String> contents;
private final SharedType.TagPosition position;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void parseMyConstants() {
component -> {
assertThat(component.name()).isEqualTo("REFERENCED_VALUE_IN_STATIC_BLOCK");
assertThat(component.value().getValue()).isEqualTo(112L);
assertThat(component.getTagLiterals(SharedType.TargetType.GO)).anyMatch(s -> s.contains("test comments"));
assertThat(component.getTagLiterals(SharedType.TargetType.GO)).anyMatch(s -> s.getContents().contains("// test comments for inlined constant"));
},
component -> {
assertThat(component.name()).isEqualTo("REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void parseEnumSize() {

EnumValueInfo constant3 = enumSize.components().get(2);
assertThat(constant3.value().getValue()).isEqualTo(3);
assertThat(constant3.getTagLiterals(SharedType.TargetType.RUST)).containsExactly("// test comments for enum");
assertThat(constant3.getTagLiterals(SharedType.TargetType.RUST).get(0).getContents()).containsExactly("// test comments for enum");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ void objectField() {
var typeInfo = (ConcreteTypeInfo)objectField.type();
assertThat(typeInfo.qualifiedName()).isEqualTo("java.lang.Object");
assertThat(objectField.getTagLiterals(SharedType.TargetType.RUST)).satisfiesExactly(
literal1 -> assertThat(literal1).isEqualTo("// test comments for class"),
literal2 -> assertThat(literal2).startsWith("#[serde")
literal1 -> assertThat(literal1.getContents()).containsExactly("// test comments for class"),
literal2 -> assertThat(literal2.getContents().get(0)).startsWith("#[serde")
);
assertThat(objectField.getTagLiterals(SharedType.TargetType.TYPESCRIPT)).satisfiesExactly(
literal1 -> assertThat(literal1).isEqualTo("// test comments for class")
literal1 -> assertThat(literal1.getContents()).containsExactly("// test comments for class")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum EnumSize {
SMALL(1),
MEDIUM(2),
@SharedType.TagLiteral(tags = "// test comments for enum")
@SharedType.TagLiteral(tags = "// test inline comments", position = SharedType.TagPosition.INLINE_AFTER)
LARGE(3);

@JsonValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
public final class JavaClass extends SuperClassA {
static final long SOME_LONG_VALUE = 123L;

@SharedType.TagLiteral(tags = "// test comments for field")
@SharedType.TagLiteral(
tags = "// test inline comments",
position = SharedType.TagPosition.INLINE_AFTER,
targets = {SharedType.TargetType.RUST, SharedType.TargetType.TYPESCRIPT}
)
@SharedType.TagLiteral(tags = "`json:\"string,omitempty\"` //override tags", position = SharedType.TagPosition.INLINE_AFTER, targets = SharedType.TargetType.GO)
@Getter
private String string;
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class MyConstants extends IgnoredSuperClassB {

@SharedType.TagLiteral(tags = "// test comments for inlined constant")
static final long REFERENCED_VALUE_IN_STATIC_BLOCK;
@SharedType.TagLiteral(tags = "// test inline comments", position = SharedType.TagPosition.INLINE_AFTER)
static final long REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK;
static final long SELF_REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK;
static final long SELF_REFERENCED_LOCAL_VALUE_QUALIFIED_NAME_IN_STATIC_BLOCK;
Expand Down Expand Up @@ -78,6 +79,7 @@ static final class InnerConstantClass {
enum MyEnumConstants {
;
@SharedType.TagLiteral(tags = "// test comments for constant in enum type")
@SharedType.TagLiteral(tags = "// test inline comments", position = SharedType.TagPosition.INLINE_AFTER)
private static final int INT_VALUE = 1;
private static final String STR_VALUE = "abc";
}
Loading