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

Not able to check if a primitive type field (bool, string, int) has been set or not #14

Closed
pavolloffay opened this issue Nov 4, 2020 · 0 comments · Fixed by #15
Closed

Comments

@pavolloffay
Copy link
Member

I am consuming the proto in java agent and I want to provide default values for header/body capture (enabled by default) but also reporting address and secure reporting.

The reporting address is easy to set. If it is empty I will just set it to a default value. However the problem is with bool values e.g. reporting.secure and dataCapture.httpHeaders.request etc. These values I am not able to check if they have been set by the user or not. Specifically, I am not able to determine if the user explicitly set the bool to false.

FieldDescriptor requestField = httpBodyBuilder.getDescriptorForType().findFieldByName("request");
    if (!httpBodyBuilder.hasField(requestField)) { // returns always false even if the field has been set to false.
      httpBodyBuilder.setRequest(true);
    }

Here is how I create the config.

  public static AgentConfig load(String filename) throws IOException {
    File configFile = new File(filename);
    InputStream fileInputStream = new FileInputStream(configFile);
    String json = convertYamlToJson(fileInputStream);

    AgentConfig.Builder configProtoBuilder = AgentConfig.newBuilder(); // if the applyDefaults is called here the PARSER fails on com.google.protobuf.InvalidProtocolBufferException: Field AgentConfig.serviceName has already been set.
    PARSER.ignoringUnknownFields().merge(json, configProtoBuilder);
    AgentConfig agentConfig = configProtoBuilder.build();

    return applyDefaults(agentConfig.toBuilder()).build();
  

see also protocolbuffers/protobuf#359

I propose to use optional in the filed, however it is an experimental feature or use wrappers which is a recommended approach - https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wrappers.proto and https://developers.google.com/protocol-buffers/docs/reference/google.protobuf
``

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

Successfully merging a pull request may close this issue.

1 participant