Skip to content
This repository has been archived by the owner on Apr 13, 2019. It is now read-only.

Commit

Permalink
asm: renamed on to from
Browse files Browse the repository at this point in the history
map asm: map extractor now takes projection as an extra argument
  • Loading branch information
Konstantin Sobolev committed Oct 27, 2017
1 parent ec92694 commit 823ee1a
Show file tree
Hide file tree
Showing 22 changed files with 98 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class AnonListTypeNameAsmImpl extends AnonListTypeNameAsm<AnonListT

private AnonListTypeNameAsmImpl() {
super(
DataTypeNameAsmImpl.INSTANCE.on(tn -> tn.elementTypeName),
DataTypeNameAsmImpl.INSTANCE.from(tn -> tn.elementTypeName),
TypeNameAsmImpl.TYPE_NAME_ASM
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private AnonMapTypeNameAsmImpl() {
super(
new AnonMapTypeNameRecordAsm<>(
TypeNameAsmImpl.INSTANCE,
DataTypeNameAsmImpl.INSTANCE.on(tn -> tn.valueTypeName)
DataTypeNameAsmImpl.INSTANCE.from(tn -> tn.valueTypeName)
),
TypeNameAsmImpl.TYPE_NAME_ASM
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public final class DataTypeAsmImpl extends DataTypeAsm<DataTypeApi> {
private DataTypeAsmImpl() {

super(
TagAsmImpl.INSTANCE.on(DataTypeApi::retroTag),
TypeAsmImpl.INSTANCE.on(DataTypeApi::type)
TagAsmImpl.INSTANCE.from(DataTypeApi::retroTag),
TypeAsmImpl.INSTANCE.from(DataTypeApi::type)
);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private DataTypeNameAsmImpl() {
.<NameString.Value>map(retro -> NameString.create(retro).asValue())
.orElse(NameString.type.createValue(null))
),
TypeNameAsmImpl.INSTANCE.on(dtn -> dtn.typeName)
TypeNameAsmImpl.INSTANCE.from(dtn -> dtn.typeName)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ public DatumTypeAsmImpl() {
throw new RuntimeException("unknown kind: " + t.kind());
}
},
TypeAsmImpl.ABSTRACT_ASM.on(t -> (DatumTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.on(DatumTypeApi::annotations), // annotations
INSTANCE.on(DatumTypeApi::metaType), // meta
TypeNameAsmImpl.INSTANCE.on(DatumTypeApi::name), // name
TypeAsmImpl.ABSTRACT_ASM.from(t -> (DatumTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.from(DatumTypeApi::annotations), // annotations
INSTANCE.from(DatumTypeApi::metaType), // meta
TypeNameAsmImpl.INSTANCE.from(DatumTypeApi::name), // name
new DatumType_ListAsm<>(DatumTypeApi::supertypes, INSTANCE), // supertypes
//tails
RecordTypeAsmImpl.INSTANCE.on(t -> (RecordTypeApi) t), // record
MapTypeAsmImpl.INSTANCE.on(t -> (MapTypeApi) t), // map
ListTypeAsmImpl.INSTANCE.on(t -> (ListTypeApi) t), // list
PrimitiveTypeAsmImpl.INSTANCE.on(t -> (PrimitiveTypeApi) t) // primitive
RecordTypeAsmImpl.INSTANCE.from(t -> (RecordTypeApi) t), // record
MapTypeAsmImpl.INSTANCE.from(t -> (MapTypeApi) t), // map
ListTypeAsmImpl.INSTANCE.from(t -> (ListTypeApi) t), // list
PrimitiveTypeAsmImpl.INSTANCE.from(t -> (PrimitiveTypeApi) t) // primitive
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public final class EntityTypeAsmImpl extends EntityTypeAsm<EntityTypeApi> {

private EntityTypeAsmImpl() {
super(
TypeAsmImpl.ABSTRACT_ASM.on(t -> (EntityTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.on(EntityTypeApi::annotations), // annotations
QualifiedTypeNameAsmImpl.INSTANCE.on(EntityTypeApi::name), // name
TypeAsmImpl.ABSTRACT_ASM.from(t -> (EntityTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.from(EntityTypeApi::annotations), // annotations
QualifiedTypeNameAsmImpl.INSTANCE.from(EntityTypeApi::name), // name
new EntityType_ListAsm<>(EntityTypeApi::supertypes, INSTANCE), // supertypes
new Tag_ListAsm<>(EntityTypeApi::tags, TagAsmImpl.INSTANCE) // tags
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public final class FieldAsmImpl extends Field_Asm<FieldApi> {

private FieldAsmImpl() {
super(
AnnotationsAsmImpl.INSTANCE.on(FieldApi::annotations),
AnnotationsAsmImpl.INSTANCE.from(FieldApi::annotations),
(f, p, c) -> FieldName.create().setString(NameString.create(f.name())),
DataTypeAsmImpl.INSTANCE.on(FieldApi::dataType)
DataTypeAsmImpl.INSTANCE.from(FieldApi::dataType)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public final class ListTypeAsmImpl extends ListTypeAsm<ListTypeApi> {

private ListTypeAsmImpl() {
super(
TypeAsmImpl.ABSTRACT_ASM.on(t -> (ListTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.on(ListTypeApi::annotations), // annotations
DatumTypeAsmImpl.INSTANCE.on(ListTypeApi::metaType), // meta
TypeNameAsmImpl.INSTANCE.on(ListTypeApi::name), // name
TypeAsmImpl.ABSTRACT_ASM.from(t -> (ListTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.from(ListTypeApi::annotations), // annotations
DatumTypeAsmImpl.INSTANCE.from(ListTypeApi::metaType), // meta
TypeNameAsmImpl.INSTANCE.from(ListTypeApi::name), // name
new ListType_ListAsm<>(ListTypeApi::supertypes, INSTANCE), // supertypes
DataTypeAsmImpl.INSTANCE.on(ListTypeApi::elementType) // element type
DataTypeAsmImpl.INSTANCE.from(ListTypeApi::elementType) // element type
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public final class MapTypeAsmImpl extends MapTypeAsm<MapTypeApi> {

private MapTypeAsmImpl() {
super(
TypeAsmImpl.ABSTRACT_ASM.on(t -> (MapTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.on(MapTypeApi::annotations), // annotations
DatumTypeAsmImpl.INSTANCE.on(MapTypeApi::keyType), // key type
DatumTypeAsmImpl.INSTANCE.on(MapTypeApi::metaType), // meta
TypeNameAsmImpl.INSTANCE.on(MapTypeApi::name), // name
TypeAsmImpl.ABSTRACT_ASM.from(t -> (MapTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.from(MapTypeApi::annotations), // annotations
DatumTypeAsmImpl.INSTANCE.from(MapTypeApi::keyType), // key type
DatumTypeAsmImpl.INSTANCE.from(MapTypeApi::metaType), // meta
TypeNameAsmImpl.INSTANCE.from(MapTypeApi::name), // name
new MapType_ListAsm<>(MapTypeApi::supertypes, INSTANCE), // supertypes
DataTypeAsmImpl.INSTANCE.on(MapTypeApi::valueType) // value type
DataTypeAsmImpl.INSTANCE.from(MapTypeApi::valueType) // value type
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import epigraph.schema.NameString;
import ws.epigraph.services._resources.epigraph.operations.read.types.output.NameString_Type_MapAsm;
import ws.epigraph.types.TypeApi;
import ws.epigraph.util.Function2;

import java.util.Map;
import java.util.function.Function;

/**
* @author <a href="mailto:[email protected]">Konstantin Sobolev</a>
Expand All @@ -34,7 +34,7 @@ public final class NameString_Type_MapAsmImpl
private NameString_Type_MapAsmImpl() {
super(
NameString::create,
Function.identity(),
Function2.identity1(),
TypeAsmImpl.INSTANCE
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public final class PrimitiveTypeAsmImpl extends PrimitiveTypeAsm<PrimitiveTypeAp

private PrimitiveTypeAsmImpl() {
super(
TypeAsmImpl.ABSTRACT_ASM.on(t -> (PrimitiveTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.on(PrimitiveTypeApi::annotations), // annotations
DatumTypeAsmImpl.INSTANCE.on(PrimitiveTypeApi::metaType), // meta
QualifiedTypeNameAsmImpl.INSTANCE.on(PrimitiveTypeApi::name), // name
TypeAsmImpl.ABSTRACT_ASM.from(t -> (PrimitiveTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.from(PrimitiveTypeApi::annotations), // annotations
DatumTypeAsmImpl.INSTANCE.from(PrimitiveTypeApi::metaType), // meta
QualifiedTypeNameAsmImpl.INSTANCE.from(PrimitiveTypeApi::name), // name
new PrimitiveType_ListAsm<>(PrimitiveTypeApi::supertypes, INSTANCE) // supertypes
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public final class RecordTypeAsmImpl extends RecordTypeAsm<RecordTypeApi> {

private RecordTypeAsmImpl() {
super(
TypeAsmImpl.ABSTRACT_ASM.on(t -> (RecordTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.on(RecordTypeApi::annotations), // annotations
TypeAsmImpl.ABSTRACT_ASM.from(t -> (RecordTypeApi) t), // abstract
AnnotationsAsmImpl.INSTANCE.from(RecordTypeApi::annotations), // annotations
new Field_ListAsm<>(RecordTypeApi::immediateFields, FieldAsmImpl.INSTANCE), // declared fields
DatumTypeAsmImpl.INSTANCE.on(RecordTypeApi::metaType), // meta
QualifiedTypeNameAsmImpl.INSTANCE.on(RecordTypeApi::name), // name
DatumTypeAsmImpl.INSTANCE.from(RecordTypeApi::metaType), // meta
QualifiedTypeNameAsmImpl.INSTANCE.from(RecordTypeApi::name), // name
new RecordType_ListAsm<>(RecordTypeApi::supertypes, INSTANCE) // supertypes
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public final class TagAsmImpl extends Tag_Asm<TagApi> {

private TagAsmImpl() {
super(
AnnotationsAsmImpl.INSTANCE.on(TagApi::annotations),
AnnotationsAsmImpl.INSTANCE.from(TagApi::annotations),
(tag, projection, ctx) -> TagName.create().setString(NameString.create(tag.name())),
DatumTypeAsmImpl.INSTANCE.on(TagApi::type)
DatumTypeAsmImpl.INSTANCE.from(TagApi::type)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ private TypeAsmImpl() {
super(
t -> (t.kind() == TypeKind.ENTITY) ? epigraph.schema.EntityType.type : epigraph.schema.DatumType.type, // (output) type extractor
ABSTRACT_ASM, // abstract
AnnotationsAsmImpl.INSTANCE.on(TypeApi::annotations), // annotations
TypeNameAsmImpl.INSTANCE.on(TypeApi::name), // name
AnnotationsAsmImpl.INSTANCE.from(TypeApi::annotations), // annotations
TypeNameAsmImpl.INSTANCE.from(TypeApi::name), // name
new Type_ListAsm<>(TypeApi::supertypes, TypeAsmImpl.INSTANCE), // supertypes
//tails
EntityTypeAsmImpl.INSTANCE.on(t -> (EntityTypeApi) t), // entity
DatumTypeAsmImpl.INSTANCE.on(t -> (DatumTypeApi) t) // datum
EntityTypeAsmImpl.INSTANCE.from(t -> (EntityTypeApi) t), // entity
DatumTypeAsmImpl.INSTANCE.from(t -> (DatumTypeApi) t) // datum
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ private TypeNameAsmImpl() {
else throw new IllegalArgumentException(tn.getClass().getName());
},
TYPE_NAME_ASM,
QualifiedTypeNameAsmImpl.INSTANCE.on(tn -> (QualifiedTypeName) tn),
AnonListTypeNameAsmImpl.INSTANCE.on(tn -> (AnonListTypeName) tn),
AnonMapTypeNameAsmImpl.INSTANCE.on(tn -> (AnonMapTypeName) tn)
QualifiedTypeNameAsmImpl.INSTANCE.from(tn -> (QualifiedTypeName) tn),
AnonListTypeNameAsmImpl.INSTANCE.from(tn -> (AnonListTypeName) tn),
AnonMapTypeNameAsmImpl.INSTANCE.from(tn -> (AnonMapTypeName) tn)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import ws.epigraph.examples.library._resources.books.operations.read._default.output.BookId_BookRecord_MapAsm;
import ws.epigraph.examples.library._resources.books.operations.read._default.output.OutputBooksFieldProjection;
import ws.epigraph.schema.operations.ReadOperationDeclaration;
import ws.epigraph.util.Function2;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
Expand All @@ -42,7 +43,7 @@ public class AsmBasedBooksReadOperation extends AbstractReadOperation {
BookId_BookRecord_Map.Value value =
new BookId_BookRecord_MapAsm<>(
Function.identity(), // key converter
Function.identity(), // map extractor
Function2.identity1(), // map extractor
BookRecordAsmImpl.INSTANCE // items assembler
).assemble(BooksBackend.getBooks(), projection.dataProjection(), new AsmContext());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class BookRecordAsmImpl extends BookRecordAsm<BooksBackend.BookData
private BookRecordAsmImpl() {
super(
// author
AuthorAsmImpl.INSTANCE.on(bd -> AuthorsBackend.get(bd.authorId)),
AuthorAsmImpl.INSTANCE.from(bd -> AuthorsBackend.get(bd.authorId)),

// text
new TextAsm<>(PlainTextAsm.INSTANCE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MapAsmGen(
private val kt = importManager.use(lqn2(keyCType, nsString))
private val it = importManager.use(lqn2(itemCType, nsString))
private val elementGenName = importManager.use(g.elementGen.fullClassName)
private val fun2 = importManager.use(classOf[ws.epigraph.util.Function2[_, _, _]].getName)

import Imports._

Expand All @@ -62,7 +63,7 @@ else {
$t.Builder b = $t.create();
ctx.visited.put(key, b.asValue());
$elementGenName itemsProjection = p.itemsProjection();
$mp<K, ? extends V> map = mapExtractor.apply(dto);
$mp<K, ? extends V> map = mapExtractor.apply(dto, p);
for ($mp.Entry<K, ? extends V> entry: map.entrySet()) {
$kt k = keyConverter.apply(entry.getKey());
b.put${if (isEntity) "$" else "_"}(k, itemAsm.assemble(entry.getValue(), itemsProjection, ctx));
Expand Down Expand Up @@ -96,7 +97,7 @@ ${JavaGenUtils.generatedAnnotation(this)}
public class $shortClassName<D, K, V> implements $assembler<D, $notNull $projectionName, $notNull $t.Value> {
${if (hasTails) s" private final $notNull $func<? super D, ? extends Type> typeExtractor;\n" else "" }\
private final $notNull $keysConverterType keyConverter;
private final $notNull $func<D, $mp<K, ? extends V>> mapExtractor;
private final $notNull $fun2<D, $projectionName, $mp<K, ? extends V>> mapExtractor;
private final $notNull $itemAsmType itemAsm;
${if (hasTails) tps.map { tp => s" private final $notNull ${tp.assemblerType} ${tp.assembler};"}.mkString("\n //tail assemblers\n","\n","") else "" }\
${if (hasMeta) s" //meta assembler\n private final $notNull $metaAsmType metaAsm;" else ""}
Expand All @@ -114,7 +115,7 @@ ${if (hasMeta) s"\n * @param metaAsm metadata assembler" else ""}
public $shortClassName(
${if (hasTails) s" $notNull $func<? super D, ? extends Type> typeExtractor,\n" else "" }\
$notNull $keysConverterType keyConverter,
$notNull $func<D, $mp<K, ? extends V>> mapExtractor,
$notNull $fun2<D, $projectionName, $mp<K, ? extends V>> mapExtractor,
$notNull $itemAsmType itemAsm\
${if (hasTails) tps.map { tp => s" $notNull ${tp.assemblerType} ${tp.assembler}"}.mkString(",\n", ",\n", "") else ""}\
${if (hasMeta) s",\n $notNull $metaAsmType metaAsm" else ""}
Expand Down
4 changes: 2 additions & 2 deletions java/core/src/main/java/ws/epigraph/assembly/Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface Asm<D, P, R> {
*
* @return composed assembler
*/
default <T> @NotNull Asm<T, P, R> on(@NotNull Function<T, D> f) {
default <T> @NotNull Asm<T, P, R> from(@NotNull Function<T, D> f) {
return (dto, projection, ctx) -> assemble(f.apply(dto), projection, ctx);
}

Expand All @@ -62,7 +62,7 @@ public interface Asm<D, P, R> {
*
* @return composed assembler
*/
default <T> @NotNull Asm<T, P, R> on(@NotNull Function<T, D> f, @NotNull Function<RuntimeException, R> ef) {
default <T> @NotNull Asm<T, P, R> from(@NotNull Function<T, D> f, @NotNull Function<RuntimeException, R> ef) {
return (dto, projection, ctx) -> {
D d;
try {
Expand Down
33 changes: 33 additions & 0 deletions java/util/src/main/java/ws/epigraph/util/Function2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2017 Sumo Logic
*
* 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 ws.epigraph.util;

/**
* @author <a href="mailto:[email protected]">Konstantin Sobolev</a>
*/
@FunctionalInterface
public interface Function2<A, B, C> {
C apply(A a, B b);

static <T, A> Function2<T, A, T> identity1() {
return (x, a) -> x;
}

static <T, A> Function2<A, T, T> identity2() {
return (x, a) -> a;
}
}
7 changes: 7 additions & 0 deletions java/util/src/main/java/ws/epigraph/util/Tuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package ws.epigraph.util;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand All @@ -32,6 +33,12 @@ public Tuple2(final T1 _1, final T2 _2) {
this._2 = _2;
}

@Contract(pure = true)
public T1 first() { return _1; }

@Contract(pure = true)
public T2 second() { return _2; }

public static @NotNull <T1, T2> Tuple2<T1, T2> of(T1 _1, T2 _2) { return new Tuple2<>(_1, _2); }

@Override
Expand Down
1 change: 1 addition & 0 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@

# Cleanup
- [ ] check out artifactory-tools to clean up old light-psi versions
- see `ArtifactoryClient`
- [ ] DataType(Api): type should be EntityType ?
- [ ] Rename Data/Var/Union type to some common name

Expand Down

0 comments on commit 823ee1a

Please sign in to comment.