Skip to content

Commit

Permalink
🐳 refactor(GenerateDoc): Improved generated documentation about broad…
Browse files Browse the repository at this point in the history
…casting
  • Loading branch information
iohao committed Nov 1, 2024
1 parent c563cc5 commit b69225c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.iohao.game.action.skeleton.core.CmdInfo;
import com.iohao.game.action.skeleton.protocol.wrapper.ByteValueList;
import com.iohao.game.action.skeleton.protocol.wrapper.WrapperKit;
import com.iohao.game.common.kit.StrKit;
import lombok.AccessLevel;
import lombok.Setter;
import lombok.experimental.Accessors;
Expand Down Expand Up @@ -72,14 +73,22 @@ public BroadcastDocumentBuilder setDataClassList(Class<?> dataClass) {
}

public BroadcastDocumentBuilder setDataClassList(Class<?> dataClass, String dataDescription) {
String simpleName = ByteValueList.class.getSimpleName();
String simpleNameActualClazz = dataClass.getSimpleName();

this.dataClassName = String.format("%s<%s>", simpleName, simpleNameActualClazz);
this.list = true;
this.dataClass = dataClass;
this.dataDescription = dataDescription;

WrapperKit.optionalValueRecord(dataClass).ifPresentOrElse(valueRecord -> {
this.dataClassName = valueRecord.getValueListClazz().getSimpleName();

if (StrKit.isEmpty(dataDescription)) {
this.dataDescription = this.dataClassName;
}
}, () -> {
String simpleName = ByteValueList.class.getSimpleName();
String simpleNameActualClazz = dataClass.getSimpleName();
this.dataClassName = String.format("%s<%s>", simpleName, simpleNameActualClazz);
});

return this;
}

Expand All @@ -102,13 +111,16 @@ public BroadcastDocumentBuilder setDataClass(Class<?> dataClass) {
*/
public BroadcastDocumentBuilder setDataClass(Class<?> dataClass, String dataDescription) {

this.dataClass = dataClass;
this.dataDescription = dataDescription;

this.dataClassName = WrapperKit.optionalRefType(dataClass)
.map(Class::getSimpleName)
.orElse(dataClass.getSimpleName());
WrapperKit.optionalValueRecord(dataClass).ifPresentOrElse(valueRecord -> {
this.dataClassName = valueRecord.getValueClazz().getSimpleName();

this.dataClass = dataClass;
if (StrKit.isEmpty(dataDescription)) {
this.dataDescription = this.dataClassName;
}
}, () -> this.dataClassName = dataClass.getSimpleName());

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package com.iohao.game.action.skeleton.core.doc;

import com.iohao.game.action.skeleton.core.CmdInfo;
import com.iohao.game.common.kit.StrKit;
import com.iohao.game.common.kit.io.FileKit;
import com.iohao.game.common.kit.time.FormatTimeKit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.action.skeleton.protocol.wrapper;

import lombok.Getter;

/**
* Wrapper values contain single and list classes
*
* @author 渔民小镇
* @date 2024-11-01
* @since 21.20
*/
@Getter
public final class ValueRecord {
final Class<?> valueClazz;
final Class<?> valueListClazz;

ValueRecord(Class<?> valueClazz, Class<?> valueListClazz) {
this.valueClazz = valueClazz;
this.valueListClazz = valueListClazz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,36 @@ public boolean isWrapper(Class<?> clazz) {
return wrapperTypeSet.contains(clazz);
}

final Map<Class<?>, Class<?>> refTypeMap = new HashMap<>();
final Map<Class<?>, ValueRecord> refTypeMap = new HashMap<>();

static {
refTypeMap.put(int.class, IntValue.class);
refTypeMap.put(Integer.class, IntValue.class);
refTypeMap.put(IntValue.class, IntValue.class);

refTypeMap.put(long.class, LongValue.class);
refTypeMap.put(Long.class, LongValue.class);
refTypeMap.put(LongValue.class, LongValue.class);

refTypeMap.put(boolean.class, BoolValue.class);
refTypeMap.put(Boolean.class, BoolValue.class);
refTypeMap.put(BoolValue.class, BoolValue.class);

refTypeMap.put(String.class, StringValue.class);
refTypeMap.put(StringValue.class, StringValue.class);
var intRecord = new ValueRecord(IntValue.class, IntValueList.class);
refTypeMap.put(int.class, intRecord);
refTypeMap.put(Integer.class, intRecord);
refTypeMap.put(IntValue.class, intRecord);

var longRecord = new ValueRecord(LongValue.class, LongValueList.class);
refTypeMap.put(long.class, longRecord);
refTypeMap.put(Long.class, longRecord);
refTypeMap.put(LongValue.class, longRecord);

var boolRecord = new ValueRecord(BoolValue.class, BoolValueList.class);
refTypeMap.put(boolean.class, boolRecord);
refTypeMap.put(Boolean.class, boolRecord);
refTypeMap.put(BoolValue.class, boolRecord);

var stringRecord = new ValueRecord(StringValue.class, StringValueList.class);
refTypeMap.put(String.class, stringRecord);
refTypeMap.put(StringValue.class, stringRecord);
}

@Deprecated
public Optional<Class<?>> optionalRefType(Class<?> clazz) {
return Optional.ofNullable(refTypeMap.get(clazz)).map(ValueRecord::getClass);
}

public Optional<ValueRecord> optionalValueRecord(Class<?> clazz) {
return Optional.ofNullable(refTypeMap.get(clazz));
}

}

0 comments on commit b69225c

Please sign in to comment.