Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #898 from zalando/aruha-1528-audience
Browse files Browse the repository at this point in the history
Change audience from underscore to dash
  • Loading branch information
rcillo authored Jun 28, 2018
2 parents b4bf11a + 66eb25f commit bbfb446
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
10 changes: 5 additions & 5 deletions docs/_data/nakadi-event-bus-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2510,11 +2510,11 @@ definitions:
audience:
type: string
x-extensible-enum:
- component_internal
- business_unit_internal
- company_internal
- external_partner
- external_public
- component-internal
- business-unit-internal
- company-internal
- external-partner
- external-public
description: |
Intended target audience of the event type. Relevant for standards around quality of design and documentation,
reviews, discoverability, changeability, and permission granting. See the guidelines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void userJourneyM1() throws InterruptedException, IOException {
.body("name", equalTo(eventTypeName))
.body("owning_application", equalTo(owningApp))
.body("category", equalTo("undefined"))
.body("audience", equalTo("external_public"))
.body("audience", equalTo("external-public"))
.body("ordering_key_fields", equalTo(Lists.newArrayList("foo", "bar.baz")))
.body("schema.type", equalTo("json_schema"))
.body("schema.schema", equalTo("{\"type\": \"object\", \"properties\": {\"foo\": " +
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/org/zalando/nakadi/config/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.zalando.nakadi.domain.Audience;
import org.zalando.problem.ProblemModule;

import java.io.IOException;

import static com.fasterxml.jackson.databind.PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES;
import static com.fasterxml.jackson.databind.PropertyNamingStrategy.SNAKE_CASE;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
Expand All @@ -35,7 +36,7 @@ public class JsonConfig {
@Primary
public ObjectMapper jacksonObjectMapper() {
final ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
.setPropertyNamingStrategy(SNAKE_CASE);

objectMapper.registerModule(enumModule());
objectMapper.registerModule(new Jdk8Module());
Expand Down Expand Up @@ -75,12 +76,24 @@ public Enum deserialize(final JsonParser jp, final DeserializationContext ctxt)
@SuppressWarnings("unchecked")
final Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass();
final String jpValueAsString = jp.getValueAsString();

try {
return Enum.valueOf(rawClass, jpValueAsString.toUpperCase());
if (rawClass.equals(Audience.class)) {
return Audience.fromString(jpValueAsString);
} else {
return Enum.valueOf(rawClass, jpValueAsString.toUpperCase());
}
} catch (final IllegalArgumentException e) {
final String possibleValues = stream(rawClass.getEnumConstants())
.map(enumValue -> enumValue.name().toLowerCase())
.collect(joining(", "));
final String possibleValues;
if (rawClass.equals(Audience.class)) {
possibleValues = stream(rawClass.getEnumConstants())
.map(enumValue -> enumValue.name().toLowerCase().replaceAll("_", "-"))
.collect(joining(", "));
} else {
possibleValues = stream(rawClass.getEnumConstants())
.map(enumValue -> enumValue.name().toLowerCase())
.collect(joining(", "));
}
throw new JsonMappingException("Illegal enum value: '" + jpValueAsString
+ "'. Possible values: [" + possibleValues + "]");
}
Expand All @@ -95,7 +108,11 @@ private static class LowerCaseEnumJsonSerializer extends StdSerializer<Enum> {
@Override
public void serialize(final Enum value, final JsonGenerator jgen, final SerializerProvider provider)
throws IOException {
jgen.writeString(value.name().toLowerCase());
if (value.getClass().equals(Audience.class)) {
jgen.writeString(((Audience)value).getText());
} else {
jgen.writeString(value.name().toLowerCase());
}
}
}
}
25 changes: 24 additions & 1 deletion src/main/java/org/zalando/nakadi/domain/Audience.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package org.zalando.nakadi.domain;

public enum Audience {
COMPONENT_INTERNAL, BUSINESS_UNIT_INTERNAL, COMPANY_INTERNAL, EXTERNAL_PARTNER, EXTERNAL_PUBLIC
COMPONENT_INTERNAL("component-internal"),
BUSINESS_UNIT_INTERNAL("business-unit-internal"),
COMPANY_INTERNAL("company-internal"),
EXTERNAL_PARTNER("external-partner"),
EXTERNAL_PUBLIC("external-public");

private String text;

Audience(final String text) {
this.text = text;
}

public String getText() {
return this.text;
}

public static Audience fromString(final String text) {
for (final Audience audience : Audience.values()) {
if (audience.text.equals(text)) {
return audience;
}
}
throw new IllegalArgumentException();
}
}
2 changes: 1 addition & 1 deletion src/test/resources/sample-event-type-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"options" : {
"retention_time": 86400000
},
"audience": "external_public"
"audience": "external-public"
}
2 changes: 1 addition & 1 deletion src/test/resources/sample-event-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"type": "json_schema",
"schema": "{\"type\": \"object\", \"properties\": {\"foo\": {\"type\": \"string\"}, \"bar\": {\"type\": \"object\", \"properties\": {\"baz\": {\"type\": \"string\"}}}}, \"required\": [\"foo\"]}"
},
"audience": "external_public",
"audience": "external-public",
"ordering_key_fields": ["foo", "bar.baz"]
}

0 comments on commit bbfb446

Please sign in to comment.