All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.auties.protobuf.utils.ProtobufUtils Maven / Gradle / Ivy

There is a newer version: 1.16
Show newest version
package it.auties.protobuf.utils;

import it.auties.protobuf.ast.FieldStatement;
import it.auties.protobuf.ast.OneOfStatement;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

import javax.lang.model.SourceVersion;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Objects;

@UtilityClass
public class ProtobufUtils {
    // This method is used inside the Message Model generator
    @SuppressWarnings("unused")
    public String toValidIdentifier(String identifier){
        return SourceVersion.isKeyword(identifier) ? "_%s".formatted(identifier) : identifier;
    }

    @SneakyThrows
    public String readGenerator(String name){
        var stream = ProtobufUtils.class.getClassLoader().getResourceAsStream("%s.java".formatted(name));
        return new String(Objects.requireNonNull(stream).readAllBytes(), StandardCharsets.UTF_8);
    }

    // This method is used inside the Message Model generator
    @SuppressWarnings("unused")
    public String generateCondition(String oneOfName, Iterator statements){
        var next = statements.next();
        return """
                if(%s != null) return %s.%s;
                %s
                """.formatted(next.getName(), oneOfName, next.getNameAsConstant(), statements.hasNext() ? generateCondition(oneOfName, statements) : "return %s.UNKNOWN;".formatted(oneOfName));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy