-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
393 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...defs-doclet/src/test/java/com/vertispan/tsdefs/tests/tsdocs/doclet/branded/Direction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright © 2023 Vertispan | ||
* | ||
* Licensed 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. | ||
*/ | ||
package com.vertispan.tsdefs.tests.tsdocs.doclet.branded; | ||
|
||
import jsinterop.annotations.JsType; | ||
|
||
/** Branded types enum test */ | ||
@JsType | ||
public enum Direction { | ||
LEFT, | ||
RIGHT, | ||
UP, | ||
DOWN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
jsinterop-ts-defs-impl/src/main/java/com/vertispan/tsdefs/impl/model/TsBrand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright © 2023 Vertispan | ||
* | ||
* Licensed 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. | ||
*/ | ||
package com.vertispan.tsdefs.impl.model; | ||
|
||
import static com.vertispan.tsdefs.impl.Formatting.NEW_LINE; | ||
|
||
public class TsBrand { | ||
private final String name; | ||
private final String namespace; | ||
private final TsType type; | ||
|
||
public TsBrand(String name, String namespace) { | ||
this.name = name; | ||
this.namespace = namespace; | ||
this.type = new TsType(name + "Type", namespace); | ||
} | ||
|
||
public String emit(String indent) { | ||
StringBuffer sb = new StringBuffer(); | ||
sb.append(indent); | ||
sb.append("const " + name + "__brand: unique symbol"); | ||
sb.append(NEW_LINE); | ||
sb.append(indent); | ||
sb.append("type Brand<B> = { [" + name + "__brand]: B }"); | ||
sb.append(NEW_LINE); | ||
sb.append(indent); | ||
sb.append("export type Branded<T, B> = T & Brand<B>"); | ||
sb.append(NEW_LINE); | ||
sb.append(indent); | ||
sb.append("type " + name + "Type = Branded<string, \"" + name + "\">;"); | ||
sb.append(NEW_LINE); | ||
sb.append(indent); | ||
return sb.toString(); | ||
} | ||
|
||
public TsType getType() { | ||
return this.type; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
jsinterop-ts-defs-impl/src/main/java/com/vertispan/tsdefs/impl/model/TsBrandedType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright © 2023 Vertispan | ||
* | ||
* Licensed 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. | ||
*/ | ||
package com.vertispan.tsdefs.impl.model; | ||
|
||
import static com.vertispan.tsdefs.impl.Formatting.NEW_LINE; | ||
|
||
import com.vertispan.tsdefs.impl.builders.HasDocs; | ||
import com.vertispan.tsdefs.impl.builders.HasFunctions; | ||
import com.vertispan.tsdefs.impl.builders.HasNamespace; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
public class TsBrandedType implements HasNamespace { | ||
private final String name; | ||
private final String namespace; | ||
private TsType type; | ||
private final Set<TsModifier> modifiers = new LinkedHashSet<>(); | ||
private final Set<String> enumerations = new LinkedHashSet<>(); | ||
private final Set<TsMethod> functions = new LinkedHashSet<>(); | ||
private TsDoc tsDoc; | ||
private boolean deprecated; | ||
|
||
private TsBrandedType(String name, String namespace, TsType type) { | ||
this.name = name; | ||
this.namespace = namespace; | ||
this.type = type; | ||
} | ||
|
||
public static TsBrandedType.TsBrandedTypeBuilder builder( | ||
String name, String namespace, TsType type) { | ||
return new TsBrandedType.TsBrandedTypeBuilder(name, namespace, type); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
public TsType getType() { | ||
return type; | ||
} | ||
|
||
public String emit(String indent, String parentNamespace) { | ||
StringBuffer sb = new StringBuffer(); | ||
|
||
TsBrand tsBrand = new TsBrand(name, namespace); | ||
sb.append(tsBrand.emit(indent)); | ||
sb.append(NEW_LINE); | ||
|
||
TsClass.TsClassBuilder classBuilder = | ||
TsClass.builder(name, namespace).addModifiers(TsModifier.EXPORT).setDocs(tsDoc); | ||
enumerations.stream() | ||
.map( | ||
enumeration -> | ||
TsProperty.builder(enumeration, tsBrand.getType()) | ||
.setDocs(TsDoc.empty()) | ||
.addModifiers(TsModifier.STATIC, TsModifier.READONLY) | ||
.build()) | ||
.forEach(classBuilder::addProperty); | ||
|
||
functions.forEach(classBuilder::addFunction); | ||
|
||
sb.append(classBuilder.build().emit(indent, parentNamespace)); | ||
|
||
return sb.toString(); | ||
} | ||
|
||
public static class TsBrandedTypeBuilder | ||
implements HasDocs<TsBrandedType.TsBrandedTypeBuilder>, | ||
HasFunctions<TsBrandedType.TsBrandedTypeBuilder> { | ||
private final TsBrandedType tsEnum; | ||
|
||
private TsBrandedTypeBuilder(String name, String namespace, TsType type) { | ||
this.tsEnum = new TsBrandedType(name, namespace, type); | ||
} | ||
|
||
public TsBrandedType.TsBrandedTypeBuilder addEnumeration(String name) { | ||
this.tsEnum.enumerations.add(name); | ||
return this; | ||
} | ||
|
||
public TsBrandedType.TsBrandedTypeBuilder addModifiers(TsModifier... modifiers) { | ||
this.tsEnum.modifiers.addAll(Arrays.asList(modifiers)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public TsBrandedType.TsBrandedTypeBuilder setDocs(TsDoc tsDoc) { | ||
this.tsEnum.tsDoc = tsDoc; | ||
return this; | ||
} | ||
|
||
public TsBrandedType.TsBrandedTypeBuilder setDeprecated(boolean deprecated) { | ||
this.tsEnum.deprecated = deprecated; | ||
return this; | ||
} | ||
|
||
@Override | ||
public TsBrandedType.TsBrandedTypeBuilder addFunction(TsMethod function) { | ||
this.tsEnum.functions.add(function); | ||
return this; | ||
} | ||
|
||
public TsBrandedType build() { | ||
return this.tsEnum; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.