-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make generator non descriptor-specific
- Loading branch information
Showing
33 changed files
with
406 additions
and
238 deletions.
There are no files selected for viewing
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
4 changes: 4 additions & 0 deletions
4
generator/src/main/java/org/sudu/protogen/descriptors/Descriptor.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,4 @@ | ||
package org.sudu.protogen.descriptors; | ||
|
||
public interface Descriptor { | ||
} |
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
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
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
35 changes: 35 additions & 0 deletions
35
generator/src/main/java/org/sudu/protogen/generator/DescriptorGenerator.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,35 @@ | ||
package org.sudu.protogen.generator; | ||
|
||
import org.sudu.protogen.descriptors.Descriptor; | ||
|
||
import java.util.WeakHashMap; | ||
|
||
public interface DescriptorGenerator<D extends Descriptor, T> { | ||
|
||
T generate(D descriptor); | ||
|
||
default DescriptorGenerator<D, T> withCache() { | ||
return new CachedGenerator<>(this); | ||
} | ||
|
||
class CachedGenerator<D extends Descriptor, T> implements DescriptorGenerator<D, T> { | ||
|
||
private final WeakHashMap<D, T> cache; | ||
|
||
private final DescriptorGenerator<D, T> generator; | ||
|
||
public CachedGenerator(DescriptorGenerator<D, T> generator, WeakHashMap<D, T> cache) { | ||
this.cache = cache; | ||
this.generator = generator; | ||
} | ||
|
||
public CachedGenerator(DescriptorGenerator<D, T> generator) { | ||
this(generator, new WeakHashMap<>()); | ||
} | ||
|
||
public final T generate(D descriptor) { | ||
return cache.computeIfAbsent(descriptor, generator::generate); | ||
} | ||
} | ||
|
||
} |
30 changes: 0 additions & 30 deletions
30
generator/src/main/java/org/sudu/protogen/generator/EnumOrMessageGenerator.java
This file was deleted.
Oops, something went wrong.
89 changes: 78 additions & 11 deletions
89
generator/src/main/java/org/sudu/protogen/generator/GenerationContext.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 |
---|---|---|
@@ -1,23 +1,90 @@ | ||
package org.sudu.protogen.generator; | ||
|
||
import com.squareup.javapoet.TypeSpec; | ||
import org.sudu.protogen.config.Configuration; | ||
import org.sudu.protogen.descriptors.EnumOrMessage; | ||
import org.sudu.protogen.descriptors.Field; | ||
import org.sudu.protogen.descriptors.Enum; | ||
import org.sudu.protogen.descriptors.*; | ||
import org.sudu.protogen.generator.client.ClientGenerator; | ||
import org.sudu.protogen.generator.enumeration.EnumGenerator; | ||
import org.sudu.protogen.generator.field.FieldGenerator; | ||
import org.sudu.protogen.generator.field.FieldProcessingResult; | ||
import org.sudu.protogen.generator.field.processors.FieldTypeProcessor; | ||
import org.sudu.protogen.generator.message.MessageGenerator; | ||
import org.sudu.protogen.generator.server.ServiceGenerator; | ||
import org.sudu.protogen.generator.type.TypeModel; | ||
import org.sudu.protogen.generator.type.processors.TypeProcessor; | ||
|
||
public record GenerationContext( | ||
Configuration configuration, | ||
TypeProcessor typeProcessor, | ||
FieldTypeProcessor fieldTypeProcessor | ||
) { | ||
public final class GenerationContext { | ||
|
||
public TypeModel processType(EnumOrMessage enumOrMessage) { | ||
return typeProcessor.processType(enumOrMessage, configuration); | ||
private final Configuration configuration; | ||
private final GeneratorsHolder holder; | ||
private final TypeManager typeManager; | ||
|
||
public GenerationContext(Configuration configuration) { | ||
this.configuration = configuration; | ||
this.holder = new GeneratorsHolder(); | ||
this.typeManager = new TypeManager(); | ||
} | ||
|
||
public Configuration configuration() { | ||
return configuration; | ||
} | ||
|
||
public GeneratorsHolder generatorsHolder() { | ||
return holder; | ||
} | ||
|
||
public TypeManager typeManager() { | ||
return typeManager; | ||
} | ||
|
||
public class TypeManager { | ||
|
||
private final FieldTypeProcessor fieldTypeProcessor = FieldTypeProcessor.Chain.getProcessingChain(GenerationContext.this); | ||
|
||
private final TypeProcessor typeProcessor = TypeProcessor.Chain.getProcessingChain(GenerationContext.this); | ||
|
||
public TypeModel processType(EnumOrMessage enumOrMessage) { | ||
return typeProcessor.processType(enumOrMessage); | ||
} | ||
|
||
public TypeModel processType(Field field) { | ||
return fieldTypeProcessor.processType(field); | ||
} | ||
} | ||
|
||
public TypeModel processType(Field field) { | ||
return fieldTypeProcessor.processType(field, this); | ||
public class GeneratorsHolder { | ||
|
||
private final DescriptorGenerator<Field, FieldProcessingResult> fieldGenerator = new FieldGenerator(GenerationContext.this).withCache(); | ||
private final DescriptorGenerator<Message, TypeSpec> messageGenerator = new MessageGenerator(GenerationContext.this).withCache(); | ||
private final DescriptorGenerator<Enum, TypeSpec> enumGenerator = new EnumGenerator(GenerationContext.this).withCache(); | ||
private final DescriptorGenerator<Service, TypeSpec> clientGenerator = new ClientGenerator(GenerationContext.this).withCache(); | ||
private final DescriptorGenerator<Service, TypeSpec> serviceGenerator = new ServiceGenerator(GenerationContext.this).withCache(); | ||
|
||
public FieldProcessingResult field(Field field) { | ||
return fieldGenerator.generate(field); | ||
} | ||
|
||
public TypeSpec generate(Enum anEnum) { | ||
return enumGenerator.generate(anEnum); | ||
} | ||
|
||
public TypeSpec generate(Message message) { | ||
return messageGenerator.generate(message); | ||
} | ||
|
||
public TypeSpec generate(EnumOrMessage enumOrMessage) { | ||
if (enumOrMessage instanceof Enum en) return generate(en); | ||
if (enumOrMessage instanceof Message msg) return generate(msg); | ||
throw new IllegalStateException(); | ||
} | ||
|
||
public TypeSpec generateClient(Service service) { | ||
return clientGenerator.generate(service); | ||
} | ||
|
||
public TypeSpec generateService(Service service) { | ||
return serviceGenerator.generate(service); | ||
} | ||
} | ||
} |
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.