Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-902.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add UserAgents.of(UserAgent, Iterable<Agent>)
links:
- https://github.com/palantir/conjure-java-runtime-api/pull/902
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.util.List;
import java.util.Optional;
import org.immutables.value.Value;
Expand Down Expand Up @@ -86,13 +87,14 @@ interface Agent {

@Value.Check
default void check() {
Preconditions.checkArgument(
UserAgents.isValidName(name()), "Illegal agent name format", SafeArg.of("name", name()));
// Should never hit the following.
Preconditions.checkArgument(
UserAgents.isValidVersion(version()),
"Illegal version format. This is a bug",
SafeArg.of("version", version()));
if (!UserAgents.isValidName(name())) {
throw new SafeIllegalArgumentException("Illegal agent name format", SafeArg.of("name", name()));
}
if (!UserAgents.isValidVersion(version())) {
// Should never hit the following.
throw new SafeIllegalArgumentException(
"Illegal version format. This is a bug", SafeArg.of("version", version()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably update this to use immutables params and avoid the Agent.Builder altogether

}

static Agent of(String name, String version) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.conjure.java.api.config.service;

import com.palantir.conjure.java.api.config.service.UserAgent.Agent;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
Expand Down Expand Up @@ -46,6 +47,16 @@ public final class UserAgents {

private UserAgents() {}

/**
* Returns a user agent with the given base agent combined with specified additional informational agents.
*/
public static UserAgent of(UserAgent base, Iterable<Agent> additional) {
return ImmutableUserAgent.builder()
.from(base)
.addAllInformational(additional)
.build();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit odd given other factory methods live in the UserAgent class, however those don't take a base UserAgent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, not sure why I put it in UserAgents instead of with other factories in UserAgent.

I'm not sure if using @Value.Parameter buys us a ton in terms of either few allocations or readability as we don't currently include Guava in the service-config module, so no cheap Iterables.concat:

    static UserAgent of(UserAgent base, Iterable<Agent> additional) {
        return ImmutableUserAgent.of(
                base.nodeId(),
                base.primary(),
                Stream.concat(base.informational().stream(), StreamSupport.stream(additional.spliterator(), false))
                        .collect(Collectors.toList()));
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right -- lgtm!


/** Returns the canonical string format for the given {@link UserAgent}. */
public static String format(UserAgent userAgent) {
StringBuilder formatted = new StringBuilder(64); // preallocate larger buffer for longer agents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.palantir.conjure.java.api.config.service.UserAgent.Agent;
import com.palantir.logsafe.SafeArg;
import java.util.List;
import org.junit.jupiter.api.Test;

public class UserAgentTest {
Expand Down Expand Up @@ -59,6 +63,29 @@ public void testCorrectHeaderFormatWithoutNodeId() {
assertThat(UserAgents.format(derivedAgent)).isEqualTo("service/1.0.0 conjure/2.0.0");
}

@Test
void testPrimaryWithInformational() {
UserAgent baseUserAgent = UserAgent.of(Agent.of("service", "1.0.0"));
List<Agent> info = ImmutableList.of(Agent.of("conjure", "1.2.3"), Agent.of("jdk", "17.0.4.1"));
UserAgent first = UserAgents.of(baseUserAgent, info);
assertThat(first).satisfies(agent -> {
assertThat(agent.primary()).isEqualTo(baseUserAgent.primary());
assertThat(agent.informational()).hasSize(2).isEqualTo(info);
assertThat(UserAgents.format(agent)).isEqualTo("service/1.0.0 conjure/1.2.3 jdk/17.0.4.1");
assertThat(UserAgents.parse(UserAgents.format(agent))).isEqualTo(agent);
assertThat(UserAgents.of(agent, ImmutableList.of())).isEqualTo(agent);
});

List<Agent> moreInfo = ImmutableList.of(Agent.of("test", "4.5.6"));
assertThat(UserAgents.of(first, moreInfo)).satisfies(agent -> {
assertThat(agent.primary()).isEqualTo(baseUserAgent.primary());
assertThat(agent.informational()).hasSize(3).containsExactlyElementsOf(Iterables.concat(info, moreInfo));
assertThat(UserAgents.format(agent)).isEqualTo("service/1.0.0 conjure/1.2.3 jdk/17.0.4.1 test/4.5.6");
assertThat(UserAgents.parse(UserAgents.format(agent))).isEqualTo(agent);
assertThat(UserAgents.of(agent, ImmutableList.of())).isEqualTo(agent);
});
}

@Test
public void testInvalidServiceName() {
assertThatLoggableExceptionThrownBy(() -> UserAgent.Agent.of("invalid service name", "1.0.0"))
Expand Down