This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 990574800e64c519a6ac85dc55815dd1021acac4
Author: wujimin <[email protected]>
AuthorDate: Thu Oct 11 10:09:45 2018 +0800

    [SCB-675] unify swagger property/parameter to make generator logic simpler
---
 .../internal/converter/BodyParameterAdapter.java   | 101 +++++++++++++++++++++
 .../internal/converter/PropertyAdapter.java        |  85 +++++++++++++++++
 .../converter/SerializableParameterAdapter.java    |  69 ++++++++++++++
 .../internal/converter/SwaggerTypeAdapter.java     |  55 +++++++++++
 4 files changed, 310 insertions(+)

diff --git 
a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/BodyParameterAdapter.java
 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/BodyParameterAdapter.java
new file mode 100644
index 0000000..cf48656
--- /dev/null
+++ 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/BodyParameterAdapter.java
@@ -0,0 +1,101 @@
+/*
+ * 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.
+ */
+package org.apache.servicecomb.codec.protobuf.internal.converter;
+
+import java.util.List;
+
+import io.swagger.models.ArrayModel;
+import io.swagger.models.Model;
+import io.swagger.models.ModelImpl;
+import io.swagger.models.RefModel;
+import io.swagger.models.parameters.BodyParameter;
+import io.swagger.models.properties.ObjectProperty;
+import io.swagger.models.properties.Property;
+
+public class BodyParameterAdapter implements SwaggerTypeAdapter {
+  private final Model model;
+
+  public BodyParameterAdapter(BodyParameter parameter) {
+    this.model = parameter.getSchema();
+  }
+
+  @Override
+  public String getRefType() {
+    if (model instanceof RefModel) {
+      return ((RefModel) model).getSimpleRef();
+    }
+
+    return null;
+  }
+
+  @Override
+  public Property getArrayItem() {
+    if (model instanceof ArrayModel) {
+      return ((ArrayModel) model).getItems();
+    }
+
+    return null;
+  }
+
+  @Override
+  public Property getMapItem() {
+    if (model instanceof ModelImpl) {
+      return ((ModelImpl) model).getAdditionalProperties();
+    }
+
+    return null;
+  }
+
+  @Override
+  public List<String> getEnum() {
+    if (model instanceof ModelImpl) {
+      return ((ModelImpl) model).getEnum();
+    }
+
+    return null;
+  }
+
+  @Override
+  public String getType() {
+    if (model instanceof ModelImpl) {
+      return ((ModelImpl) model).getType();
+    }
+
+    return null;
+  }
+
+  @Override
+  public String getFormat() {
+    if (model instanceof ModelImpl) {
+      return ((ModelImpl) model).getFormat();
+    }
+
+    return null;
+  }
+
+  @Override
+  public boolean isObject() {
+    if (model instanceof ModelImpl) {
+      ModelImpl modelImpl = (ModelImpl) model;
+      return (ObjectProperty.TYPE.equals(modelImpl.getType())
+          && modelImpl.getProperties() == null
+          && modelImpl.getName() == null);
+    }
+
+    return false;
+  }
+}
diff --git 
a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/PropertyAdapter.java
 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/PropertyAdapter.java
new file mode 100644
index 0000000..c23b08f
--- /dev/null
+++ 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/PropertyAdapter.java
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+package org.apache.servicecomb.codec.protobuf.internal.converter;
+
+import java.util.List;
+
+import io.swagger.models.properties.ArrayProperty;
+import io.swagger.models.properties.MapProperty;
+import io.swagger.models.properties.ObjectProperty;
+import io.swagger.models.properties.Property;
+import io.swagger.models.properties.RefProperty;
+import io.swagger.models.properties.StringProperty;
+
+public class PropertyAdapter implements SwaggerTypeAdapter {
+  private final Property property;
+
+  public PropertyAdapter(Property property) {
+    this.property = property;
+  }
+
+  @Override
+  public String getRefType() {
+    if (property instanceof RefProperty) {
+      return ((RefProperty) property).getSimpleRef();
+    }
+
+    return null;
+  }
+
+  @Override
+  public Property getArrayItem() {
+    if (property instanceof ArrayProperty) {
+      return ((ArrayProperty) property).getItems();
+    }
+
+    return null;
+  }
+
+  @Override
+  public Property getMapItem() {
+    if (property instanceof MapProperty) {
+      return ((MapProperty) property).getAdditionalProperties();
+    }
+
+    return null;
+  }
+
+  @Override
+  public List<String> getEnum() {
+    if (property instanceof StringProperty) {
+      return ((StringProperty) property).getEnum();
+    }
+
+    return null;
+  }
+
+  @Override
+  public String getType() {
+    return property.getType();
+  }
+
+  @Override
+  public String getFormat() {
+    return property.getFormat();
+  }
+
+  @Override
+  public boolean isObject() {
+    return property instanceof ObjectProperty;
+  }
+}
diff --git 
a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/SerializableParameterAdapter.java
 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/SerializableParameterAdapter.java
new file mode 100644
index 0000000..205afa7
--- /dev/null
+++ 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/SerializableParameterAdapter.java
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+package org.apache.servicecomb.codec.protobuf.internal.converter;
+
+import java.util.List;
+
+import io.swagger.models.parameters.SerializableParameter;
+import io.swagger.models.properties.Property;
+
+public class SerializableParameterAdapter implements SwaggerTypeAdapter {
+  private final SerializableParameter parameter;
+
+  public SerializableParameterAdapter(SerializableParameter parameter) {
+    this.parameter = parameter;
+  }
+
+  @Override
+  public String getRefType() {
+    return null;
+  }
+
+  @Override
+  public Property getArrayItem() {
+    if ("array".equals(parameter.getType())) {
+      return parameter.getItems();
+    }
+    
+    return null;
+  }
+
+  @Override
+  public Property getMapItem() {
+    return null;
+  }
+
+  @Override
+  public List<String> getEnum() {
+    return parameter.getEnum();
+  }
+
+  @Override
+  public String getType() {
+    return parameter.getType();
+  }
+
+  @Override
+  public String getFormat() {
+    return parameter.getFormat();
+  }
+
+  @Override
+  public boolean isObject() {
+    return false;
+  }
+}
diff --git 
a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/SwaggerTypeAdapter.java
 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/SwaggerTypeAdapter.java
new file mode 100644
index 0000000..235ebe8
--- /dev/null
+++ 
b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/internal/converter/SwaggerTypeAdapter.java
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+package org.apache.servicecomb.codec.protobuf.internal.converter;
+
+import java.util.List;
+
+import io.swagger.models.parameters.BodyParameter;
+import io.swagger.models.parameters.SerializableParameter;
+import io.swagger.models.properties.Property;
+
+public interface SwaggerTypeAdapter {
+  static SwaggerTypeAdapter create(Object swaggerType) {
+    if (swaggerType instanceof Property) {
+      return new PropertyAdapter((Property) swaggerType);
+    }
+
+    if (swaggerType instanceof SerializableParameter) {
+      return new SerializableParameterAdapter((SerializableParameter) 
swaggerType);
+    }
+
+    if (swaggerType instanceof BodyParameter) {
+      return new BodyParameterAdapter((BodyParameter) swaggerType);
+    }
+
+    throw new IllegalStateException("not support swagger type: " + 
swaggerType.getClass().getName());
+  }
+
+  String getRefType();
+
+  Property getArrayItem();
+
+  Property getMapItem();
+
+  List<String> getEnum();
+
+  String getType();
+
+  String getFormat();
+
+  boolean isObject();
+}

Reply via email to