Skip to content
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

Support java record #27

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions samples/javarecord/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apply plugin: 'kotlin'

dependencies {
implementation project(":tempest")

testImplementation dep.miskAwsDynamodbTesting
testImplementation dep.assertj
testImplementation dep.miskTesting
testImplementation dep.junitApi
testImplementation dep.junitEngine
}

compileKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_14
}
}

compileTestKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_14
}
}

compileJava {
sourceCompatibility = JavaVersion.VERSION_14
targetCompatibility = JavaVersion.VERSION_14
}

tasks.withType(JavaCompile) {
options.compilerArgs += "--enable-preview"
}

tasks.withType(Test) {
jvmArgs += "--enable-preview"
}

tasks.withType(JavaExec) {
jvmArgs += '--enable-preview'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2021 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.cash.tempest.javarecord.musiclibrary;

import app.cash.tempest.Attribute;
import app.cash.tempest.ForIndex;
import java.time.LocalDate;
import javax.annotation.Nullable;

public record AlbumInfo(
@Attribute(name = "partition_key")
String album_token,
String album_title,
String artist_name,
LocalDate release_date,
String genre_name,
@Attribute(prefix = "INFO_")
String sort_key
) {

public AlbumInfo(
String album_token,
String album_title,
String artist_name,
LocalDate release_date,
String genre_name) {
this(album_token,
album_title,
artist_name,
release_date,
genre_name,
"");
}

public Key key() {
return new Key(album_token);
}

public static record Key(
String album_token,
String sort_key
) {
public Key(String album_token) {
this(album_token, "");
}
}

@ForIndex(name = "genre_album_index")
public static record GenreIndexOffset(
String genre_name,
@Nullable
String album_token,
// To uniquely identify an item in pagination.
@Nullable
String sort_key
) {

public GenreIndexOffset(String genre_name) {
this(genre_name, null, null);
}

public GenreIndexOffset(String genre_name, String album_token) {
this(genre_name, album_token, null);
}
}

@ForIndex(name = "artist_album_index")
public static record ArtistIndexOffset(
String artist_name,
@Nullable
String album_token,
// To uniquely identify an item in pagination.
@Nullable
String sort_key
) {

public ArtistIndexOffset(String artist_name) {
this(artist_name, null, null);
}

public ArtistIndexOffset(String artist_name, String album_token) {
this(artist_name, album_token, null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2021 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.cash.tempest.javarecord.musiclibrary;

import app.cash.tempest.Attribute;
import app.cash.tempest.ForIndex;
import java.time.Duration;
import javax.annotation.Nullable;

public record AlbumTrack(
@Attribute(name = "partition_key")
String album_token,
@Attribute(name = "sort_key", prefix = "TRACK_")
String track_token,
String track_title,
Duration run_length
) {

public Key key() {
return new Key(album_token, track_token);
}

public Long track_number() {
return Long.parseLong(track_token, 16);
}

public AlbumTrack(
String album_token,
Long track_number,
String track_title,
Duration run_length) {
this(album_token, String.format("%016x", track_number), track_title, run_length);
}

public static record Key(
String album_token,
String track_token
) {

public Key(String album_token, Long track_number) {
this(album_token, String.format("%016x", track_number));
}

public Key(String album_token) {
this(album_token, "");
}

public Long track_number() {
return Long.parseLong(track_token, 16);
}
}

@ForIndex(name = "album_track_title_index")
public static record TitleIndexOffset(
String album_token,
String track_title,
// To uniquely identify an item in pagination.
@Nullable
String track_token
) {

public TitleIndexOffset(String album_token, String track_title) {
this(album_token, track_title, null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.cash.tempest.javarecord.musiclibrary;

import app.cash.tempest.LogicalDb;

public interface MusicDb extends LogicalDb {
MusicTable music();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2021 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.cash.tempest.javarecord.musiclibrary;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@DynamoDBTable(tableName = "j_music_items")
public class MusicItem {
// All Items.
@DynamoDBHashKey
@DynamoDBIndexRangeKey(globalSecondaryIndexNames = {"genre_album_index", "artist_album_index"})
String partition_key = null;
@DynamoDBRangeKey
String sort_key = null;

// AlbumInfo.
@DynamoDBAttribute
String album_title = null;
@DynamoDBIndexHashKey(globalSecondaryIndexName = "artist_album_index")
@DynamoDBAttribute
String artist_name = null;
@DynamoDBAttribute
@DynamoDBTypeConverted(converter = LocalDateTypeConverter.class)
LocalDate release_date = null;
@DynamoDBAttribute
@DynamoDBIndexHashKey(globalSecondaryIndexName = "genre_album_index")
String genre_name = null;

// AlbumTrack.
@DynamoDBAttribute
@DynamoDBIndexRangeKey(localSecondaryIndexName = "album_track_title_index")
String track_title = null;
@DynamoDBAttribute
@DynamoDBTypeConverted(converter = DurationTypeConverter.class)
Duration run_length = null;

// PlaylistInfo.
@DynamoDBAttribute
String playlist_name = null;
@DynamoDBAttribute
Integer playlist_size = null;
@DynamoDBAttribute
@DynamoDBTypeConverted(converter = AlbumTrackKeyListTypeConverter.class)
List<AlbumTrack.Key> playlist_tracks = null;
@DynamoDBAttribute
Long playlist_version = null;

// PlaylistEntry.
@DynamoDBAttribute
String track_token = null;
}

class DurationTypeConverter implements DynamoDBTypeConverter<String, Duration> {

@Override public String convert(Duration duration) {
return duration.toString();
}

@Override public Duration unconvert(String duration) {
return Duration.parse(duration);
}
}

class LocalDateTypeConverter implements DynamoDBTypeConverter<String, LocalDate> {

@Override public String convert(LocalDate localDate) {
return localDate.toString();
}

@Override public LocalDate unconvert(String localDate) {
return LocalDate.parse(localDate);
}
}

class AlbumTrackKeyListTypeConverter
implements DynamoDBTypeConverter<AttributeValue, List<AlbumTrack.Key>> {

@Override public AttributeValue convert(List<AlbumTrack.Key> keys) {
return new AttributeValue().withL(
keys.stream()
.map(it -> new AttributeValue().withS(convert(it)))
.collect(Collectors.toList())
);
}

@Override public List<AlbumTrack.Key> unconvert(AttributeValue items) {
return items.getL().stream()
.map(it -> unconvert(it.getS()))
.collect(Collectors.toList());
}

private AlbumTrack.Key unconvert(String string) {
var parts = string.split("/");
return new AlbumTrack.Key(parts[0], parts[1]);
}

private String convert(AlbumTrack.Key key) {
return "${key.album_token}/${key.track_token}";
}
}
Loading