-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add new LocalizationValue type for displayName, description, intro fields for models, applications, toolsets, inteseptors #1117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
54a62a6
11c1ffd
387bd3a
c06b35c
5aad110
fe9ef90
da46afe
2b8ede8
980cccb
2209c7a
f3f16fe
170bfe4
4906e88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.epam.aidial.cfg.client.dto; | ||
|
|
||
| import com.epam.aidial.cfg.client.dto.databind.LocalizedValueDeserializer; | ||
| import com.epam.aidial.cfg.client.dto.databind.LocalizedValueSerializer; | ||
| import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
| import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| @Getter | ||
| @JsonDeserialize(using = LocalizedValueDeserializer.class) | ||
| @JsonSerialize(using = LocalizedValueSerializer.class) | ||
| @EqualsAndHashCode | ||
| public final class LocalizedValueDto { | ||
|
|
||
| private final String plainValue; | ||
| private final Map<String, String> localeMap; | ||
|
|
||
| private LocalizedValueDto(String plainValue, Map<String, String> localeMap) { | ||
| this.plainValue = plainValue; | ||
| this.localeMap = localeMap; | ||
| } | ||
|
|
||
| public static LocalizedValueDto of(String value) { | ||
| return value == null ? null : new LocalizedValueDto(value, null); | ||
| } | ||
|
|
||
| public static LocalizedValueDto of(Map<String, String> localeMap) { | ||
| return localeMap == null ? null : new LocalizedValueDto(null, new LinkedHashMap<>(localeMap)); | ||
| } | ||
|
|
||
| public boolean isMap() { | ||
| return localeMap != null; | ||
| } | ||
|
|
||
| public boolean isPlain() { | ||
| return plainValue != null; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return plainValue != null ? plainValue : String.valueOf(localeMap); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.epam.aidial.cfg.client.dto.databind; | ||
|
|
||
| import com.epam.aidial.cfg.client.dto.LocalizedValueDto; | ||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class LocalizedValueDeserializer extends JsonDeserializer<LocalizedValueDto> { | ||
|
|
||
| @Override | ||
| public LocalizedValueDto deserialize(JsonParser p, DeserializationContext ctx) throws IOException { | ||
| if (p.getCurrentToken() == JsonToken.VALUE_STRING) { | ||
| return LocalizedValueDto.of(p.getValueAsString()); | ||
| } | ||
|
|
||
| if (p.getCurrentToken() == JsonToken.START_OBJECT) { | ||
| Map<String, String> localeMap = new LinkedHashMap<>(); | ||
| while (p.nextToken() != JsonToken.END_OBJECT) { | ||
| String locale = p.getCurrentName(); | ||
| p.nextToken(); | ||
| localeMap.put(locale, p.getValueAsString()); | ||
| } | ||
| return LocalizedValueDto.of(localeMap); | ||
| } | ||
|
|
||
| if (p.getCurrentToken() == JsonToken.VALUE_NULL) { | ||
| return null; | ||
| } | ||
|
|
||
| return ctx.reportInputMismatch(LocalizedValueDto.class, | ||
| "Expected a string or a locale-to-value object, got %s", p.getCurrentToken()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.epam.aidial.cfg.client.dto.databind; | ||
|
|
||
| import com.epam.aidial.cfg.client.dto.LocalizedValueDto; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.JsonSerializer; | ||
| import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class LocalizedValueSerializer extends JsonSerializer<LocalizedValueDto> { | ||
|
|
||
| @Override | ||
| public void serialize(LocalizedValueDto value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
| if (!value.isMap()) { | ||
| gen.writeString(value.getPlainValue()); | ||
| return; | ||
| } | ||
|
|
||
| if (value.getLocaleMap().size() == 1) { | ||
| gen.writeString(value.getLocaleMap().values().iterator().next()); | ||
| return; | ||
| } | ||
|
|
||
| gen.writeStartObject(); | ||
| for (var entry : value.getLocaleMap().entrySet()) { | ||
| gen.writeStringField(entry.getKey(), entry.getValue()); | ||
| } | ||
| gen.writeEndObject(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.epam.aidial.cfg.client.mapper; | ||
|
|
||
| import com.epam.aidial.cfg.client.dto.LocalizedValueDto; | ||
| import com.epam.aidial.cfg.model.LocalizedValue; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class LocalizedValueClientMapper { | ||
|
|
||
| public LocalizedValueDto toDto(LocalizedValue domain) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need this custom mapping? Won't regular mapstruct auto-mapping work? |
||
| if (domain == null) { | ||
| return null; | ||
| } | ||
| return domain.isPlain() ? LocalizedValueDto.of(domain.getPlainValue()) : LocalizedValueDto.of(domain.getLocaleMap()); | ||
| } | ||
|
|
||
| public LocalizedValue toDomain(LocalizedValueDto dto) { | ||
| if (dto == null) { | ||
| return null; | ||
| } | ||
| return dto.isPlain() ? LocalizedValue.of(dto.getPlainValue()) : LocalizedValue.of(dto.getLocaleMap()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import com.epam.aidial.cfg.client.dto.ToolSetMetadataDto; | ||
| import com.epam.aidial.cfg.client.dto.ToolSetResourceDto; | ||
| import com.epam.aidial.cfg.dto.ToolSetEximDto; | ||
| import com.epam.aidial.cfg.mapper.LocalizedValueMapper; | ||
| import com.epam.aidial.cfg.model.CreateToolSetResource; | ||
| import com.epam.aidial.cfg.model.NodeType; | ||
| import com.epam.aidial.cfg.model.ToolSetExim; | ||
|
|
@@ -19,7 +20,7 @@ | |
| import static com.epam.aidial.cfg.client.mapper.CoreMetadataUtils.extractPath; | ||
| import static com.epam.aidial.cfg.client.mapper.CoreMetadataUtils.parseEncodedVersionedPath; | ||
|
|
||
| @Mapper(componentModel = "spring") | ||
| @Mapper(componentModel = "spring", uses = {LocalizedValueClientMapper.class, LocalizedValueMapper.class}) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
| @Slf4j | ||
| public abstract class ToolSetClientMapper { | ||
| public static final String TOOLSETS_PREFIX = "toolsets/"; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| package com.epam.aidial.cfg.configuration; | ||
|
|
||
| import com.epam.aidial.cfg.configuration.databind.LocalizedValueDeserializer; | ||
| import com.epam.aidial.cfg.configuration.databind.LocalizedValueSerializer; | ||
| import com.epam.aidial.cfg.domain.model.LocalizedValue; | ||
| import com.epam.aidial.core.config.CoreCostLimit; | ||
| import com.epam.aidial.core.config.CoreCostLimitMixinForCoreObjectMapper; | ||
| import com.epam.aidial.core.config.CoreLimit; | ||
|
|
@@ -19,6 +22,7 @@ | |
| import com.fasterxml.jackson.databind.SerializationFeature; | ||
| import com.fasterxml.jackson.databind.cfg.EnumFeature; | ||
| import com.fasterxml.jackson.databind.json.JsonMapper; | ||
| import com.fasterxml.jackson.databind.module.SimpleModule; | ||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
@@ -73,6 +77,9 @@ private static JsonMapper.Builder createDefaultJsonMapperBuilder() { | |
| .addModule(new QueryLanguageModule()) | ||
| .addModule(new ValidationModule()) | ||
| .addModule(new JavaTimeModule()) | ||
| .addModule(new SimpleModule() | ||
| .addSerializer(LocalizedValue.class, new LocalizedValueSerializer()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we put serializer/deserializer right into |
||
| .addDeserializer(LocalizedValue.class, new LocalizedValueDeserializer())) | ||
| .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
| .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.epam.aidial.cfg.configuration.databind; | ||
|
|
||
| import com.epam.aidial.cfg.domain.model.LocalizedValue; | ||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class LocalizedValueDeserializer extends JsonDeserializer<LocalizedValue> { | ||
|
|
||
| @Override | ||
| public LocalizedValue deserialize(JsonParser p, DeserializationContext ctx) throws IOException { | ||
| if (p.getCurrentToken() == JsonToken.VALUE_STRING) { | ||
| return LocalizedValue.of(p.getValueAsString()); | ||
| } | ||
|
|
||
| if (p.getCurrentToken() == JsonToken.START_OBJECT) { | ||
| Map<String, String> localeMap = new LinkedHashMap<>(); | ||
| while (p.nextToken() != JsonToken.END_OBJECT) { | ||
| String locale = p.getCurrentName(); | ||
| p.nextToken(); | ||
| localeMap.put(locale, p.getValueAsString()); | ||
| } | ||
| return LocalizedValue.of(localeMap); | ||
| } | ||
|
|
||
| if (p.getCurrentToken() == JsonToken.VALUE_NULL) { | ||
| return null; | ||
| } | ||
|
|
||
| return ctx.reportInputMismatch(LocalizedValue.class, | ||
| "Expected a string or a locale-to-value object, got %s", p.getCurrentToken()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.epam.aidial.cfg.configuration.databind; | ||
|
|
||
| import com.epam.aidial.cfg.domain.model.LocalizedValue; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.JsonSerializer; | ||
| import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class LocalizedValueSerializer extends JsonSerializer<LocalizedValue> { | ||
|
|
||
| @Override | ||
| public void serialize(LocalizedValue value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
| if (!value.isMap()) { | ||
| gen.writeString(value.getPlainValue()); | ||
| return; | ||
| } | ||
|
|
||
| if (value.getLocaleMap().size() == 1) { | ||
| gen.writeString(value.getLocaleMap().values().iterator().next()); | ||
| return; | ||
| } | ||
|
|
||
| gen.writeStartObject(); | ||
| for (var entry : value.getLocaleMap().entrySet()) { | ||
| gen.writeStringField(entry.getKey(), entry.getValue()); | ||
| } | ||
| gen.writeEndObject(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,14 +9,14 @@ | |
|
|
||
| public interface ModelJpaRepository extends JpaRepository<ModelEntity, String> { | ||
|
|
||
| boolean existsByDisplayNameAndDisplayVersion(String displayName, String displayVersion); | ||
| @Query("SELECT m FROM ModelEntity m WHERE m.modelContainer IS NOT NULL") | ||
| List<ModelEntity> findByContainerIdIsNotNull(); | ||
|
|
||
| List<ModelEntity> findByIdNotIn(Collection<String> ids); | ||
|
|
||
| @Query("SELECT m FROM ModelEntity m WHERE m.modelContainer IS NOT NULL") | ||
| List<ModelEntity> findByContainerIdIsNotNull(); | ||
| List<ModelEntity> findByDisplayVersion(String displayVersion); | ||
|
|
||
| List<ModelEntity> findAllByOrderByDisplayNameAscDisplayVersionAscIdAsc(); | ||
|
|
||
| List<ModelEntity> findByIdInOrderByDisplayNameAscDisplayVersionAscIdAsc(Collection<String> ids); | ||
| List<ModelEntity> findByIdInOrderByDisplayNameAscDisplayVersionAscIdAsc(Collection<String> names); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change needed? |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
LocalizedValueMapper.classused?