Skip to content

Commit

Permalink
better jdoc for DocType
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZZank committed May 31, 2024
1 parent d2e1fb3 commit e9e9023
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/main/java/com/probejs/document/type/DocTypeResolver.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.probejs.document.type;

import com.probejs.info.type.*;
import com.probejs.util.Pair;
import com.probejs.util.StringUtil;
import lombok.val;

Expand Down Expand Up @@ -39,17 +38,17 @@ public static DocType resolve(String type) {
type = type.trim();

//TODO: Resolve object type
if (type.startsWith("{")) {
// {[x in string]: string}
if (type.startsWith("{") || type.startsWith("[")) {
// {[x in string]: string}, or [int, int, int]
return new TypeLiteral(type);
}

Pair<String, String> splitUnion = StringUtil.splitFirst(type, "<", ">", "|");
val splitUnion = StringUtil.splitFirst(type, "|");
if (splitUnion != null) {
return new TypeUnion(resolve(splitUnion.first()), resolve(splitUnion.second()));
}

Pair<String, String> splitIntersection = StringUtil.splitFirst(type, "<", ">", "&");
val splitIntersection = StringUtil.splitFirst(type, "&");
if (splitIntersection != null) {
return new TypeIntersection(
resolve(splitIntersection.first()),
Expand All @@ -62,10 +61,10 @@ public static DocType resolve(String type) {
}

if (type.endsWith(">")) {
int indexLeft = type.indexOf("<");
String rawType = type.substring(0, indexLeft);
String typeParams = type.substring(indexLeft + 1, type.length() - 1);
List<String> params = StringUtil.splitLayer(typeParams, "<", ">", ",");
val indexLeft = type.indexOf("<");
val rawType = type.substring(0, indexLeft);
val typeParams = type.substring(indexLeft + 1, type.length() - 1);
val params = StringUtil.splitLayer(typeParams, ",");
return new TypeParameterized(
resolve(rawType),
params.stream().map(DocTypeResolver::resolve).collect(Collectors.toList())
Expand All @@ -82,8 +81,8 @@ public static boolean typeEquals(DocType docType, JavaType param) {
return typeEquals(((TypeArray) docType).getBase(), array.getBase());
}
if (docType instanceof TypeParameterized && param instanceof JavaTypeParameterized parameterized) {
List<JavaType> paramInfo = parameterized.getParamTypes();
List<DocType> paramDoc = ((TypeParameterized) docType).getParamTypes();
val paramInfo = parameterized.getParamTypes();
val paramDoc = ((TypeParameterized) docType).getParamTypes();
if (paramDoc.size() != paramInfo.size()) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/probejs/document/type/TypeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import java.util.function.BiFunction;

/**
* "string[]"
* @author ZZZank
*/
@Getter
public class TypeArray implements DocType {
private final DocType base;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/probejs/document/type/TypeClazz.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.probejs.info.type.JavaTypeClass;

/**
* "String", "Map"
* note that there's no attached type variables
* @author ZZZank
*/
public class TypeClazz implements DocType {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/probejs/document/type/TypeIntersection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.util.function.BiFunction;

/**
* "Formatter & Document"
* "string & number"
* @author ZZZank
*/
public class TypeIntersection implements DocType {
private final DocType leftType;
private final DocType rightType;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/probejs/document/type/TypeLiteral.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.probejs.document.type;

/**
* string literal
* @author ZZZank
*/
public class TypeLiteral implements DocType {

private final String literal;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/probejs/document/type/TypeNamed.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.probejs.formatter.resolver.NameResolver;

/**
* also literal, but allows underscore
*/
public class TypeNamed implements DocType {

private final String typeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
* "Map<string, number>"
* @author ZZZank
*/
@Getter
public class TypeParameterized implements DocType {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/probejs/document/type/TypeUnion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.util.function.BiFunction;

/**
* "string | number"
* @author ZZZank
*/
public class TypeUnion implements DocType {
private final DocType leftType;
private final DocType rightType;
Expand Down

0 comments on commit e9e9023

Please sign in to comment.