Skip to content

Commit af562c7

Browse files
authored
Merge pull request #206 from Ecwid/common_properties_in_sealed_classes
Common properties in sealed classes
2 parents e311c13 + 31e0fbc commit af562c7

4 files changed

Lines changed: 55 additions & 38 deletions

File tree

src/main/kotlin/com/ecwid/apiclient/v3/converter/FetchedProduct.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ private fun FetchedProduct.ProductOption.CheckboxOption.toUpdated() = UpdatedPro
133133
name = name,
134134
nameTranslated = nameTranslated,
135135
choices = choices.map { it.toUpdated() },
136+
defaultChoice = defaultChoice,
136137
required = required
137138
)
138139

src/main/kotlin/com/ecwid/apiclient/v3/dto/product/request/UpdatedProduct.kt

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,60 +101,67 @@ data class UpdatedProduct(
101101
) {
102102
abstract val name: String
103103
abstract val nameTranslated: LocalizedValueMap?
104+
abstract val required: Boolean
105+
106+
interface ChoiceBased {
107+
val choices: List<ProductOptionChoice>
108+
val defaultChoice: Int?
109+
}
104110

105111
data class SelectOption(
106112
override val name: String = "",
107113
override val nameTranslated: LocalizedValueMap? = null,
108-
val choices: List<ProductOptionChoice> = listOf(),
109-
val defaultChoice: Int = 0,
110-
val required: Boolean = false
111-
) : ProductOption(ProductOptionType.SELECT)
114+
override val choices: List<ProductOptionChoice> = listOf(),
115+
override val defaultChoice: Int = 0,
116+
override val required: Boolean = false
117+
) : ProductOption(ProductOptionType.SELECT), ChoiceBased
112118

113119
data class SizeOption(
114120
override val name: String = "",
115121
override val nameTranslated: LocalizedValueMap? = null,
116-
val choices: List<ProductOptionChoice> = listOf(),
117-
val defaultChoice: Int = 0,
118-
val required: Boolean = false
119-
) : ProductOption(ProductOptionType.SIZE)
122+
override val choices: List<ProductOptionChoice> = listOf(),
123+
override val defaultChoice: Int = 0,
124+
override val required: Boolean = false
125+
) : ProductOption(ProductOptionType.SIZE), ChoiceBased
120126

121127
data class RadioOption(
122128
override val name: String = "",
123129
override val nameTranslated: LocalizedValueMap? = null,
124-
val choices: List<ProductOptionChoice> = listOf(),
125-
val defaultChoice: Int = 0,
126-
val required: Boolean = false
127-
) : ProductOption(ProductOptionType.RADIO)
130+
override val choices: List<ProductOptionChoice> = listOf(),
131+
override val defaultChoice: Int = 0,
132+
override val required: Boolean = false
133+
) : ProductOption(ProductOptionType.RADIO), ChoiceBased
128134

129135
data class CheckboxOption(
130136
override val name: String = "",
131137
override val nameTranslated: LocalizedValueMap? = null,
132-
val choices: List<ProductOptionChoice> = listOf(),
133-
val required: Boolean = false
134-
) : ProductOption(ProductOptionType.CHECKBOX)
138+
override val choices: List<ProductOptionChoice> = listOf(),
139+
override val defaultChoice: Int? = null,
140+
override val required: Boolean = false
141+
) : ProductOption(ProductOptionType.CHECKBOX), ChoiceBased
135142

136143
data class TextFieldOption(
137144
override val name: String = "",
138145
override val nameTranslated: LocalizedValueMap? = null,
139-
val required: Boolean = false
146+
override val required: Boolean = false
140147
) : ProductOption(ProductOptionType.TEXTFIELD)
141148

142149
data class TextAreaOption(
143150
override val name: String = "",
144151
override val nameTranslated: LocalizedValueMap? = null,
145-
val required: Boolean = false
152+
override val required: Boolean = false
146153
) : ProductOption(ProductOptionType.TEXTAREA)
147154

148155
data class DateOption(
149156
override val name: String = "",
150157
override val nameTranslated: LocalizedValueMap? = null,
151-
val required: Boolean = false
158+
override val required: Boolean = false
152159
) : ProductOption(ProductOptionType.DATE)
153160

154161
data class FilesOption(
155162
override val name: String = "",
156163
override val nameTranslated: LocalizedValueMap? = null,
157-
val required: Boolean = false
164+
override val required: Boolean = false
158165
) : ProductOption(ProductOptionType.FILES)
159166

160167
companion object {

src/main/kotlin/com/ecwid/apiclient/v3/dto/product/result/FetchedProduct.kt

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -158,60 +158,67 @@ data class FetchedProduct(
158158

159159
abstract val name: String
160160
abstract val nameTranslated: LocalizedValueMap?
161+
abstract val required: Boolean
162+
163+
interface ChoiceBased {
164+
val choices: List<ProductOptionChoice>
165+
val defaultChoice: Int?
166+
}
161167

162168
data class SelectOption(
163169
override val name: String = "",
164170
override val nameTranslated: LocalizedValueMap? = null,
165-
val choices: List<ProductOptionChoice> = listOf(),
166-
val defaultChoice: Int = 0,
167-
val required: Boolean = false
168-
) : ProductOption(ProductOptionType.SELECT)
171+
override val choices: List<ProductOptionChoice> = listOf(),
172+
override val defaultChoice: Int = 0,
173+
override val required: Boolean = false
174+
) : ProductOption(ProductOptionType.SELECT), ChoiceBased
169175

170176
data class SizeOption(
171177
override val name: String = "",
172178
override val nameTranslated: LocalizedValueMap? = null,
173-
val choices: List<ProductOptionChoice> = listOf(),
174-
val defaultChoice: Int = 0,
175-
val required: Boolean = false
176-
) : ProductOption(ProductOptionType.SIZE)
179+
override val choices: List<ProductOptionChoice> = listOf(),
180+
override val defaultChoice: Int = 0,
181+
override val required: Boolean = false
182+
) : ProductOption(ProductOptionType.SIZE), ChoiceBased
177183

178184
data class RadioOption(
179185
override val name: String = "",
180186
override val nameTranslated: LocalizedValueMap? = null,
181-
val choices: List<ProductOptionChoice> = listOf(),
182-
val defaultChoice: Int = 0,
183-
val required: Boolean = false
184-
) : ProductOption(ProductOptionType.RADIO)
187+
override val choices: List<ProductOptionChoice> = listOf(),
188+
override val defaultChoice: Int = 0,
189+
override val required: Boolean = false
190+
) : ProductOption(ProductOptionType.RADIO), ChoiceBased
185191

186192
data class CheckboxOption(
187193
override val name: String = "",
188194
override val nameTranslated: LocalizedValueMap? = null,
189-
val choices: List<ProductOptionChoice> = listOf(),
190-
val required: Boolean = false
191-
) : ProductOption(ProductOptionType.CHECKBOX)
195+
override val choices: List<ProductOptionChoice> = listOf(),
196+
override val defaultChoice: Int? = null,
197+
override val required: Boolean = false
198+
) : ProductOption(ProductOptionType.CHECKBOX), ChoiceBased
192199

193200
data class TextFieldOption(
194201
override val name: String = "",
195202
override val nameTranslated: LocalizedValueMap? = null,
196-
val required: Boolean = false
203+
override val required: Boolean = false
197204
) : ProductOption(ProductOptionType.TEXTFIELD)
198205

199206
data class TextAreaOption(
200207
override val name: String = "",
201208
override val nameTranslated: LocalizedValueMap? = null,
202-
val required: Boolean = false
209+
override val required: Boolean = false
203210
) : ProductOption(ProductOptionType.TEXTAREA)
204211

205212
data class DateOption(
206213
override val name: String = "",
207214
override val nameTranslated: LocalizedValueMap? = null,
208-
val required: Boolean = false
215+
override val required: Boolean = false
209216
) : ProductOption(ProductOptionType.DATE)
210217

211218
data class FilesOption(
212219
override val name: String = "",
213220
override val nameTranslated: LocalizedValueMap? = null,
214-
val required: Boolean = false
221+
override val required: Boolean = false
215222
) : ProductOption(ProductOptionType.FILES)
216223
}
217224

src/test/kotlin/com/ecwid/apiclient/v3/rule/NullablePropertyRules.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ val otherNullablePropertyRules: List<NullablePropertyRule<*, *>> = listOf(
4141
AllowNullable(FetchedProduct.ShippingPreparationTime::shippingPreparationTimeForInStockItemDays),
4242
AllowNullable(FetchedProduct.ShippingPreparationTime::shippingPreparationTimeForOutOfStockItemDays),
4343
AllowNullable(FetchedProduct.ShippingPreparationTime::localDeliveryPreparationTimeForInStockItemInMinutes),
44+
AllowNullable(FetchedProduct.ProductOption.ChoiceBased::defaultChoice),
45+
AllowNullable(FetchedProduct.ProductOption.CheckboxOption::defaultChoice),
4446

4547
IgnoreNullable(ConvertCartToOrderResult::id),
4648
IgnoreNullable(ConvertCartToOrderResult::orderNumber),

0 commit comments

Comments
 (0)