Documentation
¶
Overview ¶
Package jsonpbhelper provides version agnostic utility functions for FHIR proto conversion.
Index ¶
- Constants
- Variables
- func AddFieldToPath(jsonPath, field string) string
- func AddIndexToPath(jsonPath string, index int) string
- func AddInternalExtension(pb, ext proto.Message) error
- func AnnotateUnmarshalErrorWithPath(err error, jsonPath string) error
- func AnnotateUnmarshalErrorWithSeverity(err error, severity ErrorSeverity) error
- func AppendUnmarshalError(el *UnmarshalErrorList, newErr error) error
- func CamelToSnake(camelCase string) string
- func ExtensionFieldName(url string) string
- func ExtensionHasURL(pb proto.Message, url string) bool
- func ExtensionURL(pb protoreflect.Message) (string, error)
- func ExtensionValue(pb protoreflect.Message) (protoreflect.Message, error)
- func ExtractTimezone(t time.Time) string
- func ExtractTimezoneFromLoc(tz *time.Location) string
- func FieldMap(desc protoreflect.MessageDescriptor) map[string]protoreflect.FieldDescriptor
- func FullExtensionFieldName(url string) string
- func GetExtension(pb proto.Message, url string) (proto.Message, error)
- func GetExtensionFieldDesc(d protoreflect.MessageDescriptor) (protoreflect.FieldDescriptor, error)
- func GetField(d protoreflect.MessageDescriptor, f string) protoreflect.FieldDescriptor
- func GetInternalExtension(pb, ext proto.Message) (proto.Message, error)
- func GetLocation(tz string) (*time.Location, error)
- func GetOneofField(messageType protoreflect.MessageDescriptor, ...) (protoreflect.FieldDescriptor, error)
- func GetTimeFromUsec(us int64, tz string) (time.Time, error)
- func GetTimestampUsec(t time.Time) int64
- func HasExtension(pb proto.Message, url string) bool
- func HasInternalExtension(pb, ext proto.Message) bool
- func InternalExtensionURL(desc protoreflect.MessageDescriptor) (string, error)
- func IsChoice(d protoreflect.MessageDescriptor) bool
- func IsPrimitiveType(d protoreflect.MessageDescriptor) bool
- func IsResourceType(d protoreflect.MessageDescriptor) bool
- func IsSubmilli(s string) bool
- func IsSubmilliTime(s string) bool
- func IsSubsecond(s string) bool
- func IsSubsecondTime(s string) bool
- func IsTimeLike(pb proto.Message) bool
- func IsUnmarshalError(err error) bool
- func PrintUnmarshalError(err error, limit int) string
- func ReferenceFieldForType(resType string) (protoreflect.Name, bool)
- func RemoveExtension(pb proto.Message, url string) error
- func RemoveInternalExtension(pb, ext proto.Message) error
- func ResourceIDField(ref protoreflect.Message) (protoreflect.FieldDescriptor, error)
- func ResourceTypeForReference(resField protoreflect.Name) (string, bool)
- func SerializeTime(us int64, precision Precision) (string, error)
- func SnakeToCamel(snakeCase string) string
- func SnakeToLowerCamel(s string) string
- func UnmarshalCode(jsonPath string, in protoreflect.Message, rm json.RawMessage) (proto.Message, error)
- func ValidateReferenceType(msgField protoreflect.FieldDescriptor, ref protoreflect.Message) error
- func ValidateRequiredFields(pb protoreflect.Message) error
- func ValidateString(s string) error
- type ErrorSeverity
- type ErrorType
- type IsJSON
- type JSONArray
- type JSONObject
- type JSONRawValue
- type JSONString
- type Precision
- type Time
- type UnmarshalError
- type UnmarshalErrorList
Constants ¶
const ( // SecondToMicro records the conversion between second and microsecond. SecondToMicro = int64(time.Second) / int64(time.Microsecond) // MicroToNano records the conversion between microsecond and nanosecond. MicroToNano = int64(time.Microsecond) // UTC represents the UTC timezone string. UTC = "Z" // DefaultAnalyticsRecurExpansionDepth indicates the default max depth for recursive expansion // in marshalling to analytics schema. DefaultAnalyticsRecurExpansionDepth = 2 // LayoutYear for year layout. LayoutYear = "2006" // LayoutMonth for month layout. LayoutMonth = "2006-01" // LayoutDay for day layout. LayoutDay = "2006-01-02" // LayoutMinutesNoTZ for minute layout without timezone. LayoutMinutesNoTZ = "2006-01-02T15:04" // LayoutSecondsNoTZ for second layout without timezone. LayoutSecondsNoTZ = "2006-01-02T15:04:05" // LayoutMinutes for minute layout. LayoutMinutes = "2006-01-02T15:04-07:00" // LayoutSeconds for second layout. LayoutSeconds = "2006-01-02T15:04:05-07:00" // LayoutMillis for millisecond layout. LayoutMillis = "2006-01-02T15:04:05.000-07:00" // LayoutMicros for microsecond layout. LayoutMicros = "2006-01-02T15:04:05.000000-07:00" // LayoutMinutesUTC for minute layout plus UTC. LayoutMinutesUTC = "2006-01-02T15:04Z" // LayoutSecondsUTC for second layout plus UTC. LayoutSecondsUTC = "2006-01-02T15:04:05Z" // LayoutMillisUTC for millisecond layout plus UTC. LayoutMillisUTC = "2006-01-02T15:04:05.000Z" // LayoutMicrosUTC for microsecond layout plus UTC. LayoutMicrosUTC = "2006-01-02T15:04:05.000000Z" // LayoutTimeSecond for time layout. LayoutTimeSecond = "15:04:05" // LayoutTimeMilliSecond for millisecond time layout. LayoutTimeMilliSecond = "15:04:05.000" // LayoutTimeMicroSecond for microsecond time layout. LayoutTimeMicroSecond = "15:04:05.000000" // MidnightTimeStr for midnight time string. MidnightTimeStr = "00:00:00" // RefOneofName for oneof reference name. RefOneofName = "reference" // RefFieldSuffix for reference field suffix. RefFieldSuffix = "_id" // RefFragment for reference fragment. RefFragment = "fragment" // RefFragmentPrefix for reference fragment prefix. RefFragmentPrefix = "#" // RefHistory for reference history. RefHistory = "_history" // RefRawURI for reference raw URI. RefRawURI = "uri" // RefAnyResource is the reference ID type name that fits any FHIR resources. RefAnyResource = "Resource" // Base64BinarySeparatorStrideURL is the canonical structure definition URL // for internal extension Base64BinarySeparatorStride. Base64BinarySeparatorStrideURL = "https://g.co/fhir/StructureDefinition/base64Binary-separatorStride" // PrimitiveHasNoValueURL is the canonical structure definition URL // for internal extension PrimitiveHasNoValue. PrimitiveHasNoValueURL = "https://g.co/fhir/StructureDefinition/primitiveHasNoValue" )
const ( // ReferenceTypeError is the error occurred during reference type validation ReferenceTypeError = ErrorType("ReferenceTypeError") // RequiredFieldError is the error occurred during required field validation RequiredFieldError = ErrorType("RequiredFieldError") // ParsingError is the error occurred during json parsing ParsingError = ErrorType("ParsingError") )
const ( ErrorSeverityInformation = ErrorSeverity("informational") ErrorSeverityWarning = ErrorSeverity("warning") ErrorSeverityError = ErrorSeverity("error") )
Values for IssueSeverity.
const ( // ContainedField is the JSON field name of inline resources. ContainedField = "contained" // ResourceTypeField constant. ResourceTypeField = "resourceType" // OneofName field constant. OneofName = "oneof_resource" // Extension field constant. Extension = "extension" )
Variables ¶
var ( // SearchDateCompiledRegex for date regex used within searches only. Does // not have the correct precision for Date, DateTime, Time or Instant types. SearchDateCompiledRegex *regexp.Regexp // DateCompiledRegex for date regex. DateCompiledRegex *regexp.Regexp // DateTimeCompiledRegex for datetime regex. DateTimeCompiledRegex *regexp.Regexp // TimeCompiledRegex for time regex. TimeCompiledRegex *regexp.Regexp // InstantCompiledRegex for instant regex. InstantCompiledRegex *regexp.Regexp // PositiveIntCompiledRegex for positive integer regex. PositiveIntCompiledRegex *regexp.Regexp // UnsignedIntCompiledRegex for unsigned integer regex. UnsignedIntCompiledRegex *regexp.Regexp // JSP for JSP regex. JSP jsoniter.API // RegexValues stores the proto message full names and the regex validation // for its value fields. This map is supposed to be populated during // initialization (i.e.: func init()), once initialization is done, it should // not be modified anymore. RegexValues map[protoreflect.FullName]*regexp.Regexp )
Functions ¶
func AddFieldToPath ¶
AddFieldToPath extends a JSON path with another field.
func AddIndexToPath ¶
AddIndexToPath extends a JSON path with an index.
func AddInternalExtension ¶
AddInternalExtension adds a Google-internal extension ext if it is not existing in proto pb.
func AnnotateUnmarshalErrorWithPath ¶
AnnotateUnmarshalErrorWithPath to help the user in debugging what field caused the error.
func AnnotateUnmarshalErrorWithSeverity ¶
func AnnotateUnmarshalErrorWithSeverity(err error, severity ErrorSeverity) error
AnnotateUnmarshalErrorWithSeverity to help the user distinguish different error severity levels.
func AppendUnmarshalError ¶
func AppendUnmarshalError(el *UnmarshalErrorList, newErr error) error
AppendUnmarshalError to the current error list, or ignore it if another type of fatal error has occurred.
func CamelToSnake ¶
CamelToSnake converts a CamelCase string into snake_case by putting one '_' character before each uppercase char and lowercasing all characters.
func ExtensionFieldName ¶
ExtensionFieldName takes the extension url and returns the field name for the extension. The new field name will be the substring after the last occurrence of "/" in the url, if it is empty, the field name will be the substring between the last two occurrences of "/" in the url. Any invalid BigQuery field name characters will be replaced with an underscore, and an underscore prefix will be added if the field name does not start with a letter or underscore.
func ExtensionHasURL ¶
ExtensionHasURL checks if an extension proto has the given URL.
func ExtensionURL ¶
func ExtensionURL(pb protoreflect.Message) (string, error)
ExtensionURL returns the extension URL value.
func ExtensionValue ¶
func ExtensionValue(pb protoreflect.Message) (protoreflect.Message, error)
ExtensionValue returns the extension value proto.
func ExtractTimezone ¶
ExtractTimezone returns the timezone from time.
func ExtractTimezoneFromLoc ¶
ExtractTimezoneFromLoc returns the timezone from locale.
func FieldMap ¶
func FieldMap(desc protoreflect.MessageDescriptor) map[string]protoreflect.FieldDescriptor
FieldMap returns a lookup table for a message's fields from the FHIR JSON field names. Choice fields map to the choice message type.
func FullExtensionFieldName ¶
FullExtensionFieldName uses the url to construct a full extension field name that's compliant with BigQuery field name requirement.
func GetExtension ¶
GetExtension returns the first extension in pb whose URL matches the given url, and nil if there is no match.
func GetExtensionFieldDesc ¶
func GetExtensionFieldDesc(d protoreflect.MessageDescriptor) (protoreflect.FieldDescriptor, error)
GetExtensionFieldDesc returns the extension field descriptor.
func GetField ¶
func GetField(d protoreflect.MessageDescriptor, f string) protoreflect.FieldDescriptor
GetField returns the field descriptor.
func GetInternalExtension ¶
GetInternalExtension returns the first extension in pb whose URL matches that of extension ext from proto pb, and nil if there is no match. Only the extension type is used, and its values, if populated, in the input extension is ignored.
func GetLocation ¶
GetLocation parses tz as an IANA location or a UTC offset.
func GetOneofField ¶
func GetOneofField(messageType protoreflect.MessageDescriptor, oneofName, fieldName protoreflect.Name) (protoreflect.FieldDescriptor, error)
GetOneofField returns the oneof field, ensuring the given field is part of the given oneof.
func GetTimeFromUsec ¶
GetTimeFromUsec generates a time.Time object from the given usec and timezone.
func GetTimestampUsec ¶
GetTimestampUsec converts t to a unix timestamp.
func HasExtension ¶
HasExtension returns true iff the proto message has an extension with the given url
func HasInternalExtension ¶
HasInternalExtension returns true iff the proto message pb contains a Google-internal FHIR extension ext. Only the extension type is used, and its values, if populated, in the input extension is ignored.
func InternalExtensionURL ¶
func InternalExtensionURL(desc protoreflect.MessageDescriptor) (string, error)
InternalExtensionURL returns the internal extension URL.
func IsChoice ¶
func IsChoice(d protoreflect.MessageDescriptor) bool
IsChoice returns true iff the message type d is a FHIR choice type.
func IsPrimitiveType ¶
func IsPrimitiveType(d protoreflect.MessageDescriptor) bool
IsPrimitiveType returns true iff the message type d is a primitive FHIR data type.
func IsResourceType ¶
func IsResourceType(d protoreflect.MessageDescriptor) bool
IsResourceType returns true iff the message type d is a FHIR resource type.
func IsSubmilli ¶
IsSubmilli checks if the date input is sub-millisecond.
func IsSubmilliTime ¶
IsSubmilliTime checks if the time input is sub-millisecond.
func IsSubsecondTime ¶
IsSubsecondTime checks if the time input is sub-second.
func IsTimeLike ¶
IsTimeLike FHIR data type. These are Date, DateTime, Time and Instant.
func IsUnmarshalError ¶
IsUnmarshalError returns true if the provided error is an UnmarshalError or UnmarshalErrorList.
func PrintUnmarshalError ¶
PrintUnmarshalError to a string. May contain user data. limit controls the number of error messages that will be returned if err is an UnmarshalErrorList. Use -1 for no limit. If the error is not an UnmarshalError then Error() will be called.
func ReferenceFieldForType ¶
func ReferenceFieldForType(resType string) (protoreflect.Name, bool)
ReferenceFieldForType returns the reference field that should be populated for the supplied resource type.
func RemoveExtension ¶
RemoveExtension removes extension with given url from proto pb if it exists.
func RemoveInternalExtension ¶
RemoveInternalExtension removes extension ext from proto pb if it exists. Only the extension type is used, and its values, if populated, in the input extension is ignored.
func ResourceIDField ¶
func ResourceIDField(ref protoreflect.Message) (protoreflect.FieldDescriptor, error)
ResourceIDField returns the resource-typed field that is populated in this reference, or nil if this is another type of reference.
func ResourceTypeForReference ¶
func ResourceTypeForReference(resField protoreflect.Name) (string, bool)
ResourceTypeForReference returns the resource type that is associated with this reference field.
func SerializeTime ¶
SerializeTime serializes the values from a Time proto message to a JSON string.
func SnakeToCamel ¶
SnakeToCamel converts a snake_case string into a CamelCase string.
func SnakeToLowerCamel ¶
SnakeToLowerCamel converts the snake case to lower camel case.
func UnmarshalCode ¶
func UnmarshalCode(jsonPath string, in protoreflect.Message, rm json.RawMessage) (proto.Message, error)
UnmarshalCode interprets `rm` as a value of the enum that is the same type as `in`. `in` will not be modified.
func ValidateReferenceType ¶
func ValidateReferenceType(msgField protoreflect.FieldDescriptor, ref protoreflect.Message) error
ValidateReferenceType returns an error is `ref` is a strongly typed reference that is not compatible with the types allowed by `msgField`. References should be normalized before being passed to this function.
func ValidateRequiredFields ¶
func ValidateRequiredFields(pb protoreflect.Message) error
ValidateRequiredFields returns an error if any field isn't populated in pb that should be, according to the ValidationRequirement annotation.
func ValidateString ¶
ValidateString returns an error is the string does not conform to FHIR requirements for size and control characters.
Types ¶
type ErrorSeverity ¶
type ErrorSeverity string
ErrorSeverity represents different UnmarshalError severity levels.
type IsJSON ¶
type IsJSON interface {
// IsJSON method.
IsJSON()
}
IsJSON defines JSON related interface.
type JSONObject ¶
JSONObject defines JSON map.
type JSONRawValue ¶
type JSONRawValue []byte
JSONRawValue defines JSON bytes.
func (JSONRawValue) MarshalJSON ¶
func (v JSONRawValue) MarshalJSON() ([]byte, error)
MarshalJSON marshals JSON to bytes.
type Precision ¶
type Precision int32
Precision is used to indicate the precision of the ValueUs field of a Time.
const ( // PrecisionUnspecified indicates that the precision of the ValueUs field in a // Time is unknown. PrecisionUnspecified Precision = 0 // PrecisionSecond indicates that the precision of the ValueUs field in a Time // is to the second. PrecisionSecond Precision = 1 // PrecisionMillisecond indicates that the precision of the ValueUs field in a // Time is to the millisecond. PrecisionMillisecond Precision = 2 // PrecisionMicrosecond indicates that the precision of the ValueUs field in a // Time is to the microsecond. PrecisionMicrosecond Precision = 3 )
type Time ¶
Time contains the result of a parsed FHIR time. The fields correspond to the version-specific fields of a DateTime proto.
type UnmarshalError ¶
type UnmarshalError struct { // Path is the location where the error occurred. Path string // Details is a high level message about what the error was. This value should // not come from other libraries to ensure no PHI is reported. Details string // Diagnostics contains additional debugging information that will be appended to the end of // `Details`. This may include PHI and should not be reported where PHI is prohibited. For // example, a response is fine, but logs are not. Diagnostics string // Type is the type of the error occurred during validation. Type ErrorType // Severity represents different UnmarshalError severity levels. Severity ErrorSeverity }
UnmarshalError is a public error message for an error that occurred during unmarshaling. This type allows us to return detailed error information without exposing user data.
func (*UnmarshalError) Error ¶
func (e *UnmarshalError) Error() string
type UnmarshalErrorList ¶
type UnmarshalErrorList []*UnmarshalError
UnmarshalErrorList is a list of UnmarshalError that implements the Error interface itself.
func (UnmarshalErrorList) Error ¶
func (el UnmarshalErrorList) Error() string