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

Generated class not the same as its template #27

Open
jgagnon44 opened this issue Nov 1, 2019 · 1 comment
Open

Generated class not the same as its template #27

jgagnon44 opened this issue Nov 1, 2019 · 1 comment

Comments

@jgagnon44
Copy link

jgagnon44 commented Nov 1, 2019

I have generated a JHipster project with entities and then ran the gRPC generator. I inspected what was generated and discovered that at least one generated class does not match up with the template I assume it is based on.

For example, the MetricsService class (<package>.grpc.MetricsService.java):

package <my package name removed>.grpc;

import com.google.protobuf.Empty;
import org.lognet.springboot.grpc.GRpcService;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@GRpcService(interceptors = {AuthenticationInterceptor.class})
public class MetricService extends ReactorMetricServiceGrpc.MetricServiceImplBase {

    private final List<PublicMetrics> publicMetrics;

    /**
     * Create a new {@link MetricService} instance.
     * @param publicMetrics the metrics to expose. The collection will be sorted using the
     * {@link AnnotationAwareOrderComparator}.
     */
    public MetricService(Collection<PublicMetrics> publicMetrics) {
        Assert.notNull(publicMetrics, "PublicMetrics must not be null");
        this.publicMetrics = new ArrayList<>(publicMetrics);
        AnnotationAwareOrderComparator.sort(this.publicMetrics);
    }

    @Override
    public Flux<Metric> getMetrics(Mono<Empty> request) {
        return request
            .flatMapIterable(empty -> publicMetrics)
            .flatMapIterable(PublicMetrics::metrics)
            .map(metric -> {
                Metric.Builder builder = Metric.newBuilder()
                    .setName(metric.getName());
                if (metric.getTimestamp() != null) {
                    builder.setTimestamp(ProtobufMappers.dateToTimestamp(metric.getTimestamp()));
                }
                if (metric.getValue() instanceof Long || metric.getValue() instanceof Integer) {
                    builder.setLongValue(metric.getValue().longValue());
                } else if (metric.getValue() instanceof Float || metric.getValue() instanceof Double) {
                    builder.setDoubleValue((metric.getValue()).doubleValue());
                } else {
                    builder.setStringValue(metric.getValue().toString());
                }
                return builder.build();
            });
    }

}

While the template (_MetricService.java):

package <%= packageName %>.grpc;

import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.Empty;
import com.google.protobuf.StringValue;
import io.grpc.Status;
import org.lognet.springboot.grpc.GRpcService;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;


@GRpcService(interceptors = {AuthenticationInterceptor.class})
public class MetricService extends ReactorMetricServiceGrpc.MetricServiceImplBase {

    private final ObjectMapper mapper;

    private final MetricRegistry registry;

    public MetricService(MetricRegistry registry) {
        this.registry = registry;
        this.mapper = (new ObjectMapper()).registerModule(
            new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false, MetricFilter.ALL)
        );
    }

    @Override
    public Mono<StringValue> getMetrics(Mono<Empty> request) {
        return request
            .map(empty -> {
                try {
                    return StringValue.newBuilder().setValue(mapper.writeValueAsString(registry)).build();
                } catch (JsonProcessingException e) {
                    throw Status.INTERNAL.withCause(e).asRuntimeException();
                }
            });

    }

}

Wouldn't the generated class be a replication of the template?

@cbornet
Copy link
Owner

cbornet commented Dec 18, 2019

Which version of generator-jhipster-grpc are you using ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants