Skip to content

Commit 1a4e46f

Browse files
committed
use interface to replace instanceof code
1 parent 2b52b35 commit 1a4e46f

7 files changed

Lines changed: 120 additions & 75 deletions

File tree

src/main/groovy/com/github/hauner/openapi/spring/converter/DataTypeMapper.groovy

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
package com.github.hauner.openapi.spring.converter
1818

1919
import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
20-
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
21-
import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
22-
import com.github.hauner.openapi.spring.converter.mapping.ResponseTypeMapping
2320
import com.github.hauner.openapi.spring.converter.mapping.TargetType
2421
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
22+
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
2523
import com.github.hauner.openapi.spring.converter.schema.SchemaType
2624

2725
/**
@@ -31,66 +29,44 @@ import com.github.hauner.openapi.spring.converter.schema.SchemaType
3129
*/
3230
class DataTypeMapper {
3331

34-
private List<?> typeMappings
32+
private List<TypeMappingX> typeMappings
3533

3634
DataTypeMapper(List<?> typeMappings) {
37-
this.typeMappings = typeMappings ?: []
35+
this.typeMappings = (typeMappings ?: []) as List<TypeMappingX>
3836
}
3937

4038
TargetType getMappedDataType (SchemaType schemaType) {
4139

4240
// check endpoint mappings
43-
List<?> matchesW = schemaType.findEndpointMappings (getEndpointMappings ())
44-
if (!matchesW.empty) {
45-
TargetType target = matchesW.first().targetType
41+
List<TypeMappingX> endpointMatches = schemaType.matchEndpointMapping (typeMappings)
42+
if (!endpointMatches.empty) {
43+
TargetType target = endpointMatches.first().targetType
4644
if (target) {
4745
return target
4846
}
4947
}
5048

5149
// check global parameter & response mappings
52-
List<?> matchesX = schemaType.findGlobalMappings (getGlobalMappings ())
53-
if (!matchesX.empty) {
54-
TargetType target = matchesX.first().targetType
50+
List<TypeMappingX> ioMatches = schemaType.matchIoMapping (typeMappings)
51+
if (!ioMatches.empty) {
52+
TargetType target = ioMatches.first().targetType
5553
if (target) {
5654
return target
5755
}
5856
}
5957

6058
// check global type mapping
61-
List<?> matches = schemaType.findGlobalTypeMappings (getGlobalTypeMappings ())
62-
if (matches.isEmpty ()) {
59+
List<TypeMappingX> typeMatches = schemaType.matchTypeMapping (typeMappings)
60+
if (typeMatches.isEmpty ()) {
6361
return null
6462
}
6563

66-
if (matches.size () != 1) {
67-
throw new AmbiguousTypeMappingException (matches)
64+
if (typeMatches.size () != 1) {
65+
throw new AmbiguousTypeMappingException (typeMatches)
6866
}
6967

70-
TypeMapping match = matches.first () as TypeMapping
68+
TypeMapping match = typeMatches.first () as TypeMapping
7169
return match.targetType
7270
}
7371

74-
private List<EndpointTypeMapping> getEndpointMappings() {
75-
getEndpointMappings (typeMappings)
76-
}
77-
78-
private List<?> getGlobalMappings () {
79-
typeMappings.findResults {
80-
it instanceof ParameterTypeMapping || it instanceof ResponseTypeMapping ? it : null
81-
}
82-
}
83-
84-
private List<EndpointTypeMapping> getEndpointMappings (List<?> typeMappings) {
85-
typeMappings.findResults {
86-
it instanceof EndpointTypeMapping ? it : null
87-
}
88-
}
89-
90-
private List<TypeMapping> getGlobalTypeMappings () {
91-
typeMappings.findResults {
92-
it instanceof TypeMapping ? it : null
93-
}
94-
}
95-
9672
}

src/main/groovy/com/github/hauner/openapi/spring/converter/mapping/EndpointTypeMapping.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,19 @@ class EndpointTypeMapping implements TypeMappingX {
5050
* @param info the schema info
5151
* @return true, if path is equal, false if not
5252
*/
53+
@Override
5354
boolean matches (SchemaInfo info) {
5455
path == info.path
5556
}
5657

58+
@Override
59+
boolean isLevel (MappingLevel level) {
60+
MappingLevel.ENDPOINT == level
61+
}
62+
63+
@Override
64+
List<TypeMappingX> getChildMappings () {
65+
typeMappings
66+
}
67+
5768
}

src/main/groovy/com/github/hauner/openapi/spring/converter/mapping/ParameterTypeMapping.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ParameterTypeMapping implements TypeMappingX {
4343
* @param info a parameter schema info
4444
* @return true if it is a mapping for info, else false
4545
*/
46+
@Override
4647
boolean matches (SchemaInfo info) {
4748
if (! (info instanceof ParameterSchemaInfo)) {
4849
return false
@@ -51,4 +52,14 @@ class ParameterTypeMapping implements TypeMappingX {
5152
parameterName == info.name
5253
}
5354

55+
@Override
56+
boolean isLevel (MappingLevel level) {
57+
MappingLevel.IO == level
58+
}
59+
60+
@Override
61+
List<TypeMappingX> getChildMappings () {
62+
[mapping]
63+
}
64+
5465
}

src/main/groovy/com/github/hauner/openapi/spring/converter/mapping/ResponseTypeMapping.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ResponseTypeMapping implements TypeMappingX {
4343
* @param info a response schema info
4444
* @return true if it is a mapping for info, else false
4545
*/
46+
@Override
4647
boolean matches (SchemaInfo info) {
4748
if (! (info instanceof ResponseSchemaInfo)) {
4849
return false
@@ -51,4 +52,14 @@ class ResponseTypeMapping implements TypeMappingX {
5152
contentType == info.contentType
5253
}
5354

55+
@Override
56+
boolean isLevel (MappingLevel level) {
57+
MappingLevel.IO == level
58+
}
59+
60+
@Override
61+
List<TypeMappingX> getChildMappings () {
62+
[mapping]
63+
}
64+
5465
}

src/main/groovy/com/github/hauner/openapi/spring/converter/mapping/TypeMapping.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,27 @@ class TypeMapping implements TypeMappingX {
6161
"$sourceTypeName" + (sourceTypeFormat ? ":$sourceTypeFormat" : "")
6262
}
6363

64-
6564
/**
6665
* Checks if it is a mapping for the given schema info
6766
*
6867
* @param info a schema info
6968
* @return true if it is a mapping for info, else false
7069
*/
70+
@Override
7171
boolean matches (SchemaInfo info) {
7272
sourceTypeName == info.name
7373
}
7474

75+
@Override
76+
boolean isLevel (MappingLevel level) {
77+
MappingLevel.TYPE == level
78+
}
79+
80+
@Override
81+
List<TypeMappingX> getChildMappings () {
82+
[this]
83+
}
84+
7585
/**
7686
* Returns the target type of this type mapping.
7787
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
/*
2+
* Copyright 2019 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.github.hauner.openapi.spring.converter.mapping
218

319
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
420

21+
/**
22+
* Type mapping levels
23+
*/
24+
enum MappingLevel {
25+
ENDPOINT, IO, TYPE
26+
}
27+
28+
/**
29+
*
30+
*/
531
interface TypeMappingX {
632
boolean matches (SchemaInfo info)
33+
boolean isLevel (MappingLevel level)
34+
List<TypeMappingX> getChildMappings ()
735
}

src/main/groovy/com/github/hauner/openapi/spring/converter/schema/SchemaType.groovy

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
package com.github.hauner.openapi.spring.converter.schema
1818

19-
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
20-
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
19+
import com.github.hauner.openapi.spring.converter.mapping.MappingLevel
20+
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
2121

2222

2323
interface SchemaType {
2424

25-
List<?> findEndpointMappings (List<EndpointTypeMapping> typeMappings)
26-
List<?> findGlobalMappings (List<?> typeMappings)
27-
List<?> findGlobalTypeMappings (List<?> typeMappings)
25+
List<TypeMappingX> matchEndpointMapping (List<TypeMappingX> typeMappings)
26+
List<TypeMappingX> matchIoMapping (List<TypeMappingX> typeMappings)
27+
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings)
2828

2929
}
3030

@@ -37,42 +37,40 @@ abstract class SchemaTypeBase implements SchemaType {
3737
}
3838

3939
@Override
40-
List<?> findEndpointMappings (List<EndpointTypeMapping> typeMappings) {
41-
// endpoint mappings matching by path
42-
def all = typeMappings.findAll {
43-
it.matches (info)
40+
List<TypeMappingX> matchEndpointMapping (List<TypeMappingX> typeMappings) {
41+
// mappings matching by path
42+
List<TypeMappingX> endpoint = typeMappings.findAll {
43+
it.isLevel (MappingLevel.ENDPOINT) && it.matches (info)
4444
}.collect {
45-
it.typeMappings
46-
}.flatten ()
45+
it.childMappings
46+
}.flatten () as List<TypeMappingX>
4747

48-
// find global parameter, response & type mappings
49-
def global = all.findAll {
50-
it.matches (info)
51-
}
52-
53-
// global parameter or response mappings
54-
def mappings = global.findAll {
55-
! (it instanceof TypeMapping)
48+
// io mappings
49+
List<TypeMappingX> io = endpoint.findAll {
50+
it.isLevel (MappingLevel.IO) && it.matches (info)
5651
}.collect {
57-
it.mapping
58-
}
52+
it.childMappings
53+
}.flatten () as List<TypeMappingX>
5954

60-
if (!mappings.empty) {
61-
return mappings
55+
if (!io.empty) {
56+
return io
6257
}
6358

64-
global.findAll {
65-
it instanceof TypeMapping
66-
}
59+
// type mappings
60+
endpoint.findAll {
61+
it.isLevel (MappingLevel.TYPE) && it.matches (info)
62+
}.collect {
63+
it.childMappings
64+
}.flatten () as List<TypeMappingX>
6765
}
6866

69-
@Override
70-
List<?> findGlobalMappings (List<?> typeMappings) {
67+
List<TypeMappingX> matchIoMapping (List<TypeMappingX> typeMappings) {
68+
// io mappings
7169
typeMappings.findAll {
72-
it.matches (info)
70+
it.isLevel (MappingLevel.IO) && it.matches (info)
7371
}.collect {
74-
it.mapping
75-
}
72+
it.childMappings
73+
}.flatten () as List<TypeMappingX>
7674
}
7775

7876
}
@@ -84,9 +82,9 @@ class ObjectSchemaType extends SchemaTypeBase {
8482
}
8583

8684
@Override
87-
List<?> findGlobalTypeMappings (List<?> typeMappings) {
85+
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings) {
8886
typeMappings.findAll {
89-
it.matches (info)
87+
it.isLevel (MappingLevel.TYPE) && it.matches (info)
9088
}
9189
}
9290

@@ -99,12 +97,12 @@ class ArraySchemaType extends SchemaTypeBase {
9997
}
10098

10199
@Override
102-
List<?> findGlobalTypeMappings (List<?> typeMappings) {
100+
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings) {
103101
typeMappings.findAll () {
104-
it.matches (new SchemaInfo (null, null,'array'))
102+
it.isLevel (MappingLevel.TYPE) && it.matches (new SchemaInfo (null, null,'array'))
105103
}
106104
}
107-
105+
108106
}
109107

110108

0 commit comments

Comments
 (0)