001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.apidoc;
017
018import com.alibaba.fastjson.JSONObject;
019import io.jboot.apidoc.annotation.ApiResp;
020import io.jboot.utils.ClassType;
021import io.jboot.utils.StrUtil;
022
023import java.io.Serializable;
024import java.util.Objects;
025
026public class ApiResponse implements Serializable {
027
028    private String field;
029    private String dataType;
030    private ClassType classType;
031    private String remarks;
032    private String mock;
033
034
035    public ApiResponse() {
036    }
037
038    public ApiResponse(ApiResp apiResp) {
039        this.field = apiResp.field();
040        this.dataType = apiResp.dataType().getSimpleName();
041        this.classType = new ClassType(apiResp.dataType(), apiResp.genericTypes());
042        this.remarks = apiResp.notes();
043        this.mock = apiResp.mock();
044    }
045
046    public String getField() {
047        return field;
048    }
049
050    public void setField(String field) {
051        this.field = field;
052    }
053
054    public String getDataType() {
055        return dataType;
056    }
057
058    public void setDataType(String dataType) {
059        this.dataType = dataType;
060    }
061
062    public void setDataAndClassType(Class<?> clazz) {
063        this.dataType = clazz.getSimpleName();
064        this.classType = new ClassType(clazz);
065    }
066
067    public ClassType getClassType() {
068        return classType;
069    }
070
071    public void setClassType(ClassType classType) {
072        this.classType = classType;
073    }
074
075    public String getRemarks() {
076        return remarks;
077    }
078
079    public void setRemarks(String remarks) {
080        this.remarks = remarks;
081    }
082
083    public String getMock() {
084        return mock;
085    }
086
087    public Object getMockObject() {
088        if (StrUtil.isBlank(mock)) {
089            return "";
090        }
091        if ((mock.startsWith("{") && mock.endsWith("}")) || (mock.startsWith("[") && mock.endsWith("]"))) {
092            return JSONObject.parse(mock);
093        } else if (StrUtil.isNumeric(mock) && (Number.class.isAssignableFrom(classType.getMainClass()) || isType(int.class) || isType(long.class))) {
094            return Long.parseLong(mock);
095        } else if (StrUtil.isDecimal(mock) && (Number.class.isAssignableFrom(classType.getMainClass()) || isType(float.class) || isType(double.class))) {
096            return Double.parseDouble(mock);
097        } else {
098            return mock;
099        }
100    }
101
102    public void setMock(String mock) {
103        this.mock = mock;
104    }
105
106    @Override
107    public boolean equals(Object o) {
108        if (this == o) {
109            return true;
110        }
111        if (o == null || getClass() != o.getClass()) {
112            return false;
113        }
114        ApiResponse that = (ApiResponse) o;
115        return Objects.equals(field, that.field);
116    }
117
118    public boolean isType(Class<?> clazz) {
119        return this.classType != null && clazz == this.classType.getMainClass();
120    }
121
122
123    @Override
124    public String toString() {
125        return "ApiResponse{" +
126                "name='" + field + '\'' +
127                ", dataType='" + dataType + '\'' +
128                ", remarks='" + remarks + '\'' +
129                '}';
130    }
131}