accessor

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: 5 Imported by: 0

Documentation

Overview

Package accessor contains the getter/setter functions to access reflected FHIR proto messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendValue

func AppendValue(rpb protoreflect.Message, value interface{}, fields ...string) error

AppendValue appends the given value to the field pointed by the path specified through the given fields in the given reflected proto message. The path specified by the fields must not go through any repeated or map fields. The target field must be a repeated field (see GetList). The list can be of either primitive types (including enum) or message types. If the field is primitive type, the given value must be convertible to the field type. If the field is message type, the given value must be exactly the same type as the field. The target field must not be a map.

Example: assume we have a Foo message: foo, and its reflection: rfoo

message Foo {
 repeated int64 field_a;
	message Bar {
		repeated int64 field_b;
	}
 repeated Bar field_c;
 Bar field_d;
}

AppendValue(rfoo, 123, "field_a") append int64(123) to the foo.FieldA slice AppendValue(rfoo, &Foo_Bar{FieldB: []int64{123}}, "field_c"} append the new Foo_Bar to the foo.FieldC slice AppendValue(rfoo, "str", "field_a") returns error as value/field type mismatch AppendValue(rfoo, "str", "not_exist") returns error as field not_exist is not valid AppendValue(rfoo, 123, "field_c", "field_b") returns error as field_c is repeated as an intermediate field AppendValue(rfoo, 123, "field_d", "field_b") sets int64(123) to the foo.FieldD.FieldB slice.

func GetBytes

func GetBytes(rpb protoreflect.Message, fields ...string) ([]byte, error)

GetBytes takes a reflected proto message and a list of fields as path, returns the []byte pointed by the path. The path must not go through any repeated or map fields, and all the intermediate fields must be of message type (i.e. cannot be of primitive types). And the last field MUST field of []byte type. If the field is not populated, empty byte slice will be returned. GetBytes returns error in following cases: 1. No field is given (empty path) 2. If len(fields) > 1 and failed to get the parent message of the target []byte (see GetMessage) 3. Fields contain invalid fields 4. Target field is not of bytes kind. 5. Target field is repeated.

Example: assume we have a fully populated Foo message: foo, and its reflection: rfoo

message Foo {
 bytes field_a;
	message Bar {
		bytes field_b;
	}
 Bar field_c;
 repeated bytes field_d;
}

GetBytes(rfoo, "field_a") returns:

foo.FieldA

GetBytes(rfoo, "field_c", "field_b") returns:

foo.FieldC.FieldB

GetBytes(rfoo, "field_c") returns:

error, as field_c is not a field of bytes kind, but message kind

GetBytes(rfoo, "field_d") returns:

error, as field_d is a repeated field.

func GetEnumDescriptor

func GetEnumDescriptor(ty protoreflect.MessageDescriptor, fields ...string) (protoreflect.EnumDescriptor, error)

GetEnumDescriptor takes a proto message descriptor and a list of fields as path, returns the enum descriptor of the field pointed by the path. The path must not go through any repeated or map fields, and all the intermediate fields must be of message type (i.e. cannot be of primitive types). And the last field MUST be a field of enum type. GetEnumDescriptor returns error in following cases: 1. No field is given (empty path) 2. If len(fields) > 1 and failed to get the parent message descriptor of the target enum (see GetMessage) 3. Fields contain invalid fields 4. Target field is not of enum kind. 5. Target field is repeated.

Example: assume we have a Foo message

message Foo {
 string field_a;
	Enum Fruit {
		APPLE = 0;
   ORANGE = 1;
	}
 Fruit field_b;
 Message Bar {
   Fruit field_c
 }
 Bar field_d;
 repeated Fruit field_e;
}

GetEnumDescriptor(Foo, "field_a") returns:

error, as field_a is not a field of enum type.

GetEnumDescriptor(Foo, "field_b") returns:

the enum descriptor of Foo_Fruit

GetEnumDescriptor(Foo, "field_d", "field_c") returns:

the enum descriptor of Foo_Fruit

GetEnumDescriptor(Foo, "field_e") returns:

error, as field_e is a repeated field.

func GetEnumNumber

func GetEnumNumber(rpb protoreflect.Message, fields ...string) (protoreflect.EnumNumber, error)

GetEnumNumber takes a reflected proto message and a list of fields as path, returns the enum number pointed by the path. The path must not go through any repeated or map fields, and all the intermediate fields must be of message type (i.e. cannot be of primitive types). And the last field MUST be a field of enum type. If the field is not populated, 0 will be returned. GetEnumNumber returns error in following cases: 1. No field is given (empty path) 2. If len(fields) > 1 and failed to get the parent message of the target enum (see GetMessage) 3. Fields contain invalid fields 4. Target field is not of enum kind. 5. Target field is repeated.

Example: assume we have a fully populated Foo message: foo, and its reflection: rfoo

message Foo {
 string field_a;
	Enum Fruit {
		APPLE = 0;
   ORANGE = 1;
	}
 Fruit field_b;
 Message Bar {
   Fruit field_c
 }
 Bar field_d;
 repeated Fruit field_e;
}

GetEnumNumber(rfoo, "field_a") returns:

error, as field_a is not a field of enum type.

GetEnumNumber(rfoo, "field_b") returns:

the number value of foo.FieldB

GetEnumNumber(rfoo, "field_d", "field_c") returns:

the number value of foo.FieldD.FieldC

GetEnumNumber(rfoo, "field_e") returns:

error, as field_e is a repeated field.

func GetInt64

func GetInt64(rpb protoreflect.Message, fields ...string) (int64, error)

GetInt64 takes a reflected proto message and a list of fields as path, returns the int64 pointed by the path. The path must not go through any repeated or map fields, and all the intermediate fields must be of message type (i.e. cannot be of primitive types). And the last field MUST point to a field with one of the 64-bit int kinds. If the field is not populated, int64(0) will be returned. GetInt64 returns error in following cases: 1. No field is given (empty path) 2. If len(fields) > 1 and failed to get the parent message of the target int64 (see GetMessage) 3. Fields contain invalid fields 4. Target field is not of 64-bit int kind. 5. Target field is repeated.

Example: assume we have a fully populated Foo message: foo, and its reflection: rfoo

message Foo {
 int64 field_a;
	message Bar {
		int64 field_b;
	}
 Bar field_c;
 repeated int64 field_d;
}

GetInt64(rfoo, "field_a") returns:

foo.FieldA

GetInt64(rfoo, "field_c", "field_b") returns:

foo.FieldC.FieldB

GetInt64(rfoo, "field_c") returns:

error, as field_c is not a field of 64-bit int kind

GetInt64(rfoo, "field_d") returns:

error, as field_d is a repeated field.

func GetList

func GetList(rpb protoreflect.Message, fields ...string) (protoreflect.List, error)

GetList takes a reflected proto message and a list of field names as path, returns the nesting repeated field pointed by the path, in the form of reflected list. The field names are the names defined in the original proto message, not the ones defined in the generated Go code. The path, except the last field, must not go through any repeated or map fields, and all the intermediate fields must be of message type (i.e. cannot be of primitive types). However, the final target field can be a repeated field of either primitive types or message types. GetList returns error in following cases: 1. No field is given (empty path) 2. If len(fields) > 1 and failed to get the parent message of the target list(see GetMessage) 3. Fields contain invalid fields 4. Field pointed by the path is not of repeated

Example: assume we have a fully populated Foo message: foo, and its reflection: rfoo

message Foo {
	message Bar {
		repeated string field_a;
	}
	repeated Bar field_b;
 Bar field_c;
}

GetList(rfoo, "field_b") returns:

foo.FieldB

GetList(rfoo, "field_b", "field_a") returns:

error, as field_b is a repeated field and intermediate fields must not be repeated

GetList(rfoo, "field_c", "field_a") returns:

foo.FieldC.FieldA

func GetMessage

func GetMessage(rpb protoreflect.Message, fields ...string) (protoreflect.Message, error)

GetMessage takes a reflected proto message and a list of field names as path, returns the nesting message pointed by the path, in the form of reflected proto. The field names are the names defined in the original proto message, not the ones defined in the generated Go code. GetMessage can handle OneOf fields, given either the inner concrete field name or the oneof field name AND the inner concrete field name. GetMessage returns error in following cases:

  1. No field is given.
  2. The nested field pointed by the path is not of message type (i.e. the field is of primitive types like string, []byte, int, etc)
  3. The nested field pointed by the path is a repeated field or is a map.
  4. The path points to/through a field that does not exist.
  5. The path points to/through a field that is not populated.

Example, assume we have a fully populated Foo message: foo, and its reflection: rfoo:

message Foo {
  message Bar {
    message Xyz {
      string field_a;
    }
    Xyz field_b;
  }
  Bar field_c;
  repeated Bar field_d;
  message Bar2 {
    string bar2_content
  }
  oneof field_one_of {
    Bar field_one_of_bar;
    Bar2 field_one_of_bar2;
  }
}

GetMessage(rfoo, "field_c") returns:

reflected: foo.FieldC

GetMessage(rfoo, "field_c", "field_b") returns:

reflected: foo.FieldC.FieldB

GetMessage(rfoo, "field_c", "field_b", "field_a") returns:

error as the path points to a non-message type field

GetMessage(rfoo, "not_exist") returns:

error as not_exist is not a valid field

GetMessage(rfoo, "field_d") returns:

error as the path points to a repeated field

GetMessage(rfoo, "field_one_of", "field_one_of_bar") returns:

foo.GetFieldOneOfBar() if field_one_of_bar is populated, otherwise error

GetMessage(rfoo, "field_one_of_bar") also returns:

foo.GetFieldOneOfBar() if field_one_of_bar is populated, otherwise error

func GetString

func GetString(rpb protoreflect.Message, fields ...string) (string, error)

GetString takes a reflected proto message and a list of fields as path, returns the string pointed by the path. The path must not go through any repeated or map fields, and all the intermediate fields must be of message type (i.e. cannot be of primitive types). The last field MUST be a field of Go string type. If the string field is not populated, empty string will be returned. GetString returns error in following cases: 1. No field is given (empty path) 2. If len(fields) > 1 and failed to get the parent message of the target string (see GetMessage) 3. Fields contain invalid fields 4. Target field is not of Go string type. 5. Target field is repeated.

Example: assume we have a fully populated Foo message: foo, and its reflection: rfoo

message Foo {
 string field_a;
	message Bar {
		string field_b;
	}
 Bar field_c;
 repeated string field_d;
}

GetString(rfoo, "field_a") returns:

foo.FieldA's content

GetString(rfoo, "field_c", "field_b") returns:

foo.FieldC.FieldB's content

GetString(rfoo, "field_c") returns:

error, as field_c is not a field of string type

GetString(rfoo, "field_d") returns:

error, as field_d is a repeated field.

func GetUint32

func GetUint32(rpb protoreflect.Message, fields ...string) (uint32, error)

GetUint32 takes a reflected proto message and a list of fields as path, returns the uint32 pointed by the path. The path must not go through any repeated or map fields, and all the intermediate fields must be of message type (i.e. cannot be of primitive types). And the last field MUST point to a field with one of the 32-bit uint kinds. If the field is not populated, uint32(0) will be returned. GetUint32 returns error in following cases: 1. No field is given (empty path) 2. If len(fields) > 1 and failed to get the parent message of the target uint32 (see GetMessage) 3. Fields contain invalid fields 4. Target field is not of 32-bit uint kind. 5. Target field is repeated.

Example: assume we have a fully populated Foo message: foo, and its reflection: rfoo

message Foo {
 uint32 field_a;
	message Bar {
		uint32 field_b;
	}
 Bar field_c;
 repeated uint32 field_d;
}

GetUint32(rfoo, "field_a") returns:

foo.FieldA

GetUint32(rfoo, "field_c", "field_b") returns:

foo.FieldC.FieldB

GetUint32(rfoo, "field_c") returns:

error, as field_c is not a field of 32-bit uint kind

GetUint32(rfoo, "field_d") returns:

error, as field_d is a repeated field.

func SetValue

func SetValue(rpb protoreflect.Message, value interface{}, fields ...string) error

SetValue sets a value to a nesting field specified by the given fields in the given reflected proto message. The path specified by the fields must not go through any repeated or map fields. The target field can be primitive type field (including enum fields) or message type field. When setting primitive type field values, the given value must be convertible to the field concrete Go type. When setting message type values, the given value must be exactly the same type as the field. The target field must NOT be a repeated or map field.

Example: assume we have a Foo message: foo, and its reflection: rfoo

message Foo {
 int64 field_a;
	message Bar {
		int64 field_b;
	}
 Bar field_c;
 repeated int64 field_d;
}

SetValue(rfoo, 123, "field_a") sets the foo.FieldA to 123 SetValue(rfoo, &Foo_Bar{FieldB: 123}, "field_c"} sets the foo.FieldC to &Foo_Bar{FieldB: 123} SetValue(rfoo, 123, "field_c", "field_b") sets the foo.FieldC.FieldB to 123. And if FieldC is

not populated ahead, a Foo_Bar message will be created and set to foo.FieldC.

SetValue(rfoo, "str", "field_a") returns error as value/field type mismatch SetValue(rfoo, "str", "not_exist") returns error as field not_exist is not valid SetValue(rfoo, 123, "field_d") returns error as field_d is repeated

Types

This section is empty.

Jump to

Keyboard shortcuts

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