-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds support for generating a shared super object type (issue #7) - Add better union type handling in generator-java (issue #37) fixes #7 fixes #37
- Loading branch information
Showing
27 changed files
with
239 additions
and
266 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
spotify-web-api-core/src/main/java/de/sonallux/spotify/core/SpotifyWebApiObjectUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.sonallux.spotify.core; | ||
|
||
import de.sonallux.spotify.core.model.SpotifyWebApiObject; | ||
|
||
import java.util.List; | ||
|
||
public class SpotifyWebApiObjectUtils { | ||
private static final List<String> BASE_OBJECT_PROPERTY_NAMES = List.of("id", "type", "href", "uri"); | ||
public static final List<String> BASE_OBJECT_NAMES = List.of("AlbumObject", "ArtistObject", "EpisodeObject", "PlaylistObject", "ShowObject", "TrackObject", "UserObject"); | ||
public static final String BASE_OBJECT_NAME = "BaseObject"; | ||
public static final SpotifyWebApiObject SPOTIFY_BASE_OBJECT = new SpotifyWebApiObject(BASE_OBJECT_NAME) | ||
.addProperty(new SpotifyWebApiObject.Property("id", "String", "The [Spotify ID](https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids) for the object.")) | ||
.addProperty(new SpotifyWebApiObject.Property("type", "String", "The object type.")) | ||
.addProperty(new SpotifyWebApiObject.Property("href", "String", "A link to the Web API endpoint providing full details of the object.")) | ||
.addProperty(new SpotifyWebApiObject.Property("uri", "String", "The [Spotify URI](https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids) for the object.")); | ||
|
||
public static boolean isBaseObject(SpotifyWebApiObject object) { | ||
return object.getProperties().stream() | ||
.filter(p -> BASE_OBJECT_PROPERTY_NAMES.contains(p.getName())) | ||
.count() == BASE_OBJECT_PROPERTY_NAMES.size(); | ||
} | ||
|
||
public static boolean removeBaseProperties(SpotifyWebApiObject object) { | ||
if (BASE_OBJECT_NAME.equals(object.getName()) || !isBaseObject(object)) { | ||
return false; | ||
} | ||
object.getProperties().removeIf(p -> BASE_OBJECT_PROPERTY_NAMES.contains(p.getName())); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...r-java/src/main/java/de/sonallux/spotify/generator/java/templates/BaseObjectTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package de.sonallux.spotify.generator.java.templates; | ||
|
||
import de.sonallux.spotify.core.SpotifyWebApiObjectUtils; | ||
import de.sonallux.spotify.core.model.SpotifyWebApiObject; | ||
import de.sonallux.spotify.generator.java.util.JavaUtils; | ||
|
||
import java.util.Map; | ||
|
||
public class BaseObjectTemplate extends ObjectTemplate { | ||
@Override | ||
public String templateName() { | ||
return "base-object"; | ||
} | ||
|
||
@Override | ||
public String getFileName(SpotifyWebApiObject object) { | ||
return JavaUtils.getFileName(SpotifyWebApiObjectUtils.BASE_OBJECT_NAME); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> buildContext(SpotifyWebApiObject object, Map<String, Object> rootContext) { | ||
var context = super.buildContext(object, rootContext); | ||
context.put("className", SpotifyWebApiObjectUtils.BASE_OBJECT_NAME); | ||
|
||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
spotify-web-api-generator-java/src/main/resources/templates/base-object.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package {{package}}; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import lombok.*; | ||
|
||
{{#documentationLink}} | ||
/** | ||
* <a href="{{documentationLink}}">{{name}}</a> | ||
*/ | ||
{{/documentationLink}} | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = Album.class, name = "album"), | ||
@JsonSubTypes.Type(value = Artist.class, name = "artist"), | ||
@JsonSubTypes.Type(value = Episode.class, name = "episode"), | ||
@JsonSubTypes.Type(value = Playlist.class, name = "playlist"), | ||
@JsonSubTypes.Type(value = Show.class, name = "show"), | ||
@JsonSubTypes.Type(value = Track.class, name = "track"), | ||
@JsonSubTypes.Type(value = PrivateUser.class, name = "user"), | ||
}) | ||
public abstract class {{className}} { | ||
{{#properties}} | ||
{{#hasDescription}} | ||
/** | ||
{{#description}} | ||
* {{.}} | ||
{{/description}} | ||
*/ | ||
{{/hasDescription}} | ||
{{#nonNull}} | ||
@NonNull | ||
{{/nonNull}} | ||
{{#isReservedKeywordProperty}} | ||
@lombok.experimental.Accessors(prefix = "_") | ||
{{/isReservedKeywordProperty}} | ||
public {{type}} {{fieldName}}; | ||
{{/properties}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
spotify-web-api-java/src/main/generated/de/sonallux/spotify/api/models/BaseObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package de.sonallux.spotify.api.models; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import lombok.*; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = Album.class, name = "album"), | ||
@JsonSubTypes.Type(value = Artist.class, name = "artist"), | ||
@JsonSubTypes.Type(value = Episode.class, name = "episode"), | ||
@JsonSubTypes.Type(value = Playlist.class, name = "playlist"), | ||
@JsonSubTypes.Type(value = Show.class, name = "show"), | ||
@JsonSubTypes.Type(value = Track.class, name = "track"), | ||
@JsonSubTypes.Type(value = PrivateUser.class, name = "user"), | ||
}) | ||
public abstract class BaseObject { | ||
/** | ||
* <p>The <a href="https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids">Spotify ID</a> for the object.</p> | ||
*/ | ||
public String id; | ||
/** | ||
* <p>The object type.</p> | ||
*/ | ||
public String type; | ||
/** | ||
* <p>A link to the Web API endpoint providing full details of the object.</p> | ||
*/ | ||
public String href; | ||
/** | ||
* <p>The <a href="https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids">Spotify URI</a> for the object.</p> | ||
*/ | ||
public String uri; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.