jsonpbhelper

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2020 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package jsonpbhelper provides version agnostic utility functions for FHIR proto conversion.

Index

Constants

View Source
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"
	// 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"
	// 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"
)
View Source
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

View Source
var (
	// 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
	// IDCompiledRegex for ID regex.
	IDCompiledRegex *regexp.Regexp
	// OIDCompiledRegex for OID regex.
	OIDCompiledRegex *regexp.Regexp
	// PositiveIntCompiledRegex for positive integer regex.
	PositiveIntCompiledRegex *regexp.Regexp
	// UnsignedIntCompiledRegex for unsigned integer regex.
	UnsignedIntCompiledRegex *regexp.Regexp
	// CodeCompiledRegex for decimal regex.
	CodeCompiledRegex *regexp.Regexp
	// UUIDCompiledRegex for UUID regex.
	UUIDCompiledRegex *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 AddInternalExtension

func AddInternalExtension(pb, ext proto.Message) error

AddInternalExtension adds a Google-internal extension ext if it is not existing in proto pb.

func CamelToSnake

func CamelToSnake(camelCase string) string

CamelToSnake converts a CamelCase string into snake_case by putting one '_' character before each uppercase char and lowercasing all characters.

func ExtensionFieldName

func ExtensionFieldName(url string) string

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. 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

func ExtensionHasURL(pb proto.Message, url string) bool

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

func ExtractTimezone(t time.Time) string

ExtractTimezone returns the timezone from time.

func ExtractTimezoneFromLoc

func ExtractTimezoneFromLoc(tz *time.Location) string

ExtractTimezoneFromLoc returns the timezone from locale.

func FieldMap

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 GetExtensionFieldDesc

GetExtensionFieldDesc returns the extension field descriptor.

func GetField

GetField returns the field descriptor.

func GetInternalExtension

func GetInternalExtension(pb, ext proto.Message) (proto.Message, error)

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

func GetLocation(tz string) (*time.Location, error)

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

func GetTimeFromUsec(us int64, tz string) (time.Time, error)

GetTimeFromUsec generates a time.Time object from the given usec and timezone.

func GetTimestampUsec

func GetTimestampUsec(t time.Time) int64

GetTimestampUsec converts t to a unix timestamp.

func HasInternalExtension

func HasInternalExtension(pb, ext proto.Message) bool

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

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

func IsSubmilli(s string) bool

IsSubmilli checks if the date input is sub-millisecond.

func IsSubmilliTime

func IsSubmilliTime(s string) bool

IsSubmilliTime checks if the time input is sub-millisecond.

func IsSubsecond

func IsSubsecond(s string) bool

IsSubsecond checks if the input is sub-second.

func IsSubsecondTime

func IsSubsecondTime(s string) bool

IsSubsecondTime checks if the time input is sub-second.

func ReferenceFieldForType

func ReferenceFieldForType(resType string) (protoreflect.Name, bool)

ReferenceFieldForType returns the reference field that should be populated for the supplied resource type.

func RemoveInternalExtension

func RemoveInternalExtension(pb, ext proto.Message) error

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

func SerializeTime(us int64, precision Precision) (string, error)

SerializeTime serializes the values from a Time proto message to a JSON string.

func SnakeToCamel

func SnakeToCamel(snakeCase string) string

SnakeToCamel converts a snake_case string into a CamelCase string.

func SnakeToLowerCamel

func SnakeToLowerCamel(s string) string

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

func ValidateString(s string) error

ValidateString returns an error is the string does not conform to FHIR requirements for size and control characters.

Types

type IsJSON

type IsJSON interface {
	// IsJSON method.
	IsJSON()
}

IsJSON defines JSON related interface.

type JSONArray

type JSONArray []IsJSON

JSONArray defines JSON array.

func (JSONArray) IsJSON

func (JSONArray) IsJSON()

IsJSON implementation of JSON array.

type JSONObject

type JSONObject map[string]IsJSON

JSONObject defines JSON map.

func (JSONObject) IsJSON

func (JSONObject) IsJSON()

IsJSON implementation of JSON object.

type JSONRawValue

type JSONRawValue []byte

JSONRawValue defines JSON bytes.

func (JSONRawValue) IsJSON

func (JSONRawValue) IsJSON()

IsJSON implementation of JSON bytes.

func (JSONRawValue) MarshalJSON

func (v JSONRawValue) MarshalJSON() ([]byte, error)

MarshalJSON marshals JSON to bytes.

type JSONString

type JSONString string

JSONString defines JSON string.

func (JSONString) IsJSON

func (JSONString) IsJSON()

IsJSON implementation of JSON string.

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

type Time struct {
	Precision Precision
	ValueUs   int64
}

Time contains the result of a parsed FHIR time. The fields correspond to the version-specific fields of a DateTime proto.

func ParseTime

func ParseTime(rm []byte) (Time, error)

ParseTime parses a time into a struct that provides the time from midnight in microseconds and the precision of the original time.

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
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL