Skip to content

Commit

Permalink
1. Create primitive wrappers for FHIR R5 standard primitives.
Browse files Browse the repository at this point in the history
2. Add PrimitiveWrappersTest to test the wrappers.
3. Add txtpb necessary for the Java unit tests.

PiperOrigin-RevId: 641289304
  • Loading branch information
settyblue authored and copybara-github committed Jun 7, 2024
1 parent 5ae1b8d commit 265b40f
Show file tree
Hide file tree
Showing 29 changed files with 3,715 additions and 0 deletions.

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions java/com/google/fhir/wrappers/r5/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

java_library(
name = "primitive_wrappers",
srcs = glob(["*.java"]),
deps = [
"//java/com/google/fhir/common:annotation_utils",
"//java/com/google/fhir/common:exceptions",
"//java/com/google/fhir/common:fhir_types",
"//java/com/google/fhir/common:proto_utils",
"//proto/google/fhir/proto:annotations_java_proto",
"//proto/google/fhir/proto/r5:fhirproto_extensions_java_proto",
"//proto/google/fhir/proto/r5/core:datatypes_java_proto",
"@com_google_protobuf//:protobuf_java",
"@maven//:com_google_code_gson_gson",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)
118 changes: 118 additions & 0 deletions java/com/google/fhir/wrappers/r5/Base64BinaryWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2024 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fhir.wrappers.r5;

import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding;
import com.google.fhir.common.AnnotationUtils;
import com.google.fhir.common.ProtoUtils;
import com.google.fhir.r5.core.Base64Binary;
import com.google.fhir.r5.core.PositiveInt;
import com.google.fhir.r5.fhirproto.Base64BinarySeparatorStride;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import com.google.protobuf.MessageOrBuilder;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

/** A wrapper around the Base64Binary FHIR primitive type. */
public class Base64BinaryWrapper extends PrimitiveWrapper<Base64Binary> {

private static final Pattern REGEX_PATTERN =
Pattern.compile(
AnnotationUtils.getValueRegexForPrimitiveType(Base64Binary.getDefaultInstance()));

@Override
protected Pattern getPattern() {
return REGEX_PATTERN;
}

private static final Base64Binary NULL_BASE64_BINARY =
Base64Binary.newBuilder().addExtension(getNoValueExtension()).build();

/** Create an Base64BinaryWrapper from a Base64Binary. */
public Base64BinaryWrapper(Base64Binary base64Binary) {
super(base64Binary);
}

public Base64BinaryWrapper(MessageOrBuilder base64Binary) {
super(ProtoUtils.fieldWiseCopy(base64Binary, Base64Binary.newBuilder()).build());
}

/** Create an Base64BinaryWrapper from a java String. */
public Base64BinaryWrapper(String input) {
super(input == null ? NULL_BASE64_BINARY : parseAndValidate(input));
}

private static Base64Binary parseAndValidate(String input) {
// TODO(b/144523187): Java regex engine throws a StackOverflow exception if we try to validate
// here.

BaseEncoding encoding = BaseEncoding.base64();
Base64Binary.Builder builder = Base64Binary.newBuilder();
int stride = input.indexOf(' ');
if (stride != -1) {
int end = stride;
while (end < input.length() && input.charAt(end) == ' ') {
end++;
}
String separator = input.substring(stride, end);
Base64BinarySeparatorStride strideExtension =
Base64BinarySeparatorStride.newBuilder()
.setSeparator(com.google.fhir.r5.core.String.newBuilder().setValue(separator))
.setStride(PositiveInt.newBuilder().setValue(stride))
.build();
encoding = encoding.withSeparator(separator, stride);
ExtensionWrapper.of().add(strideExtension).addToMessage(builder);
}
try {
return builder.setValue(ByteString.copyFrom(encoding.decode(input))).build();
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid base64", e);
}
}

@Override
public void validateWrapped() {
// TODO(b/178598546): Java regex engine throws a StackOverflow exception if we try to validate
// here.
if (hasValue()) {
return;
} else {
validatePrimitiveWithoutValue();
}
}

@Override
protected String printValue() {
BaseEncoding encoding = BaseEncoding.base64();
ImmutableList<Base64BinarySeparatorStride> strideExtension =
ExtensionWrapper.fromExtensionsIn(getWrapped())
.getMatchingExtensions(Base64BinarySeparatorStride.getDefaultInstance());
if (!strideExtension.isEmpty()) {
encoding =
encoding.withSeparator(
strideExtension.get(0).getSeparator().getValue(),
strideExtension.get(0).getStride().getValue());
}
return encoding.encode(getWrapped().getValue().toByteArray());
}

@Override
protected List<Message> getInternalExtensions() {
return Collections.singletonList(Base64BinarySeparatorStride.getDefaultInstance());
}
}
66 changes: 66 additions & 0 deletions java/com/google/fhir/wrappers/r5/BooleanWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fhir.wrappers.r5;

import com.google.fhir.common.AnnotationUtils;
import com.google.fhir.common.ProtoUtils;
import com.google.fhir.r5.core.Boolean;
import com.google.gson.JsonPrimitive;
import com.google.protobuf.MessageOrBuilder;
import java.util.regex.Pattern;

/** A wrapper around the Boolean FHIR primitive type. */
public class BooleanWrapper extends PrimitiveWrapper<Boolean> {

private static final Pattern REGEX_PATTERN =
Pattern.compile(AnnotationUtils.getValueRegexForPrimitiveType(Boolean.getDefaultInstance()));

@Override
protected Pattern getPattern() {
return REGEX_PATTERN;
}

private static final Boolean NULL_BOOLEAN =
Boolean.newBuilder().addExtension(getNoValueExtension()).build();

/** Create an BooleanWrapper from a Boolean. */
public BooleanWrapper(Boolean bool) {
super(bool);
}

public BooleanWrapper(MessageOrBuilder bool) {
super(ProtoUtils.fieldWiseCopy(bool, Boolean.newBuilder()).build());
}

/** Create an BooleanWrapper from a java String. */
public BooleanWrapper(String input) {
super(input == null ? NULL_BOOLEAN : parseAndValidate(input));
}

private static Boolean parseAndValidate(String input) {
validateUsingPattern(REGEX_PATTERN, input);
return Boolean.newBuilder().setValue(java.lang.Boolean.parseBoolean(input)).build();
}

@Override
protected String printValue() {
return java.lang.Boolean.toString(getWrapped().getValue());
}

@Override
public JsonPrimitive toJson() {
return new JsonPrimitive(getWrapped().getValue());
}
}
71 changes: 71 additions & 0 deletions java/com/google/fhir/wrappers/r5/CanonicalWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2024 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fhir.wrappers.r5;

import com.google.fhir.common.AnnotationUtils;
import com.google.fhir.common.ProtoUtils;
import com.google.fhir.r5.core.Canonical;
import com.google.protobuf.MessageOrBuilder;
import java.util.regex.Pattern;

/** A wrapper around the Canonical FHIR primitive type. */
public class CanonicalWrapper extends PrimitiveWrapper<Canonical> {

private static final Pattern REGEX_PATTERN =
Pattern.compile(
AnnotationUtils.getValueRegexForPrimitiveType(Canonical.getDefaultInstance()));

@Override
protected Pattern getPattern() {
return REGEX_PATTERN;
}

private static final Canonical NULL_CANONICAL =
Canonical.newBuilder().addExtension(getNoValueExtension()).build();

/** Create an CanonicalWrapper from an Canonical. */
public CanonicalWrapper(Canonical canonical) {
super(canonical);
}

/** Create an CanonicalWrapper from an Canonical. */
public CanonicalWrapper(MessageOrBuilder message) {
super(ProtoUtils.fieldWiseCopy(message, Canonical.newBuilder()).build());
}

/** Create an CanonicalWrapper from a java String. */
public CanonicalWrapper(String input) {
super(input == null ? NULL_CANONICAL : parseAndValidate(input));
}

private static Canonical parseAndValidate(String input) {
validateUsingPattern(REGEX_PATTERN, input);
return Canonical.newBuilder().setValue(input).build();
}

@Override
protected String printValue() {
return getWrapped().getValue();
}

// Extract the uri component from a canonical, which can be of the form
// uri|version
// TODO(b/244184211): Consider separating this in the proto into value_uri and version
public static String getUri(Canonical canonical) {
String value = canonical.getValue();
int pipeIndex = value.indexOf("|");
return pipeIndex != -1 ? value.substring(0, pipeIndex) : value;
}
}
Loading

0 comments on commit 265b40f

Please sign in to comment.