ValidateValue checks the validity of the 'any' parameter which is a protobuf 'Any' type, based on the attribute type provided. 'any' is a protobuf 'Any' type which should contain a value of a specific type. 'attributeType' is an enum indicating the expected type of the value within 'any'. The funct
(any *anypb.Any, attributeType base.AttributeType)
| 307 | // |
| 308 | // The function returns nil if the value is valid (i.e., it is of the expected type and can be successfully unmarshalled). |
| 309 | func ValidateValue(any *anypb.Any, attributeType base.AttributeType) error { |
| 310 | // Declare a variable 'target' of type proto.Message to hold the unmarshalled value. |
| 311 | var target proto.Message |
| 312 | |
| 313 | // Depending on the expected attribute type, assign 'target' a new instance of the corresponding specific type. |
| 314 | switch attributeType { |
| 315 | case base.AttributeType_ATTRIBUTE_TYPE_INTEGER: |
| 316 | target = &base.IntegerValue{} |
| 317 | case base.AttributeType_ATTRIBUTE_TYPE_INTEGER_ARRAY: |
| 318 | target = &base.IntegerArrayValue{} |
| 319 | case base.AttributeType_ATTRIBUTE_TYPE_DOUBLE: |
| 320 | target = &base.DoubleValue{} |
| 321 | case base.AttributeType_ATTRIBUTE_TYPE_DOUBLE_ARRAY: |
| 322 | target = &base.DoubleArrayValue{} |
| 323 | case base.AttributeType_ATTRIBUTE_TYPE_STRING: |
| 324 | target = &base.StringValue{} |
| 325 | case base.AttributeType_ATTRIBUTE_TYPE_STRING_ARRAY: |
| 326 | target = &base.StringArrayValue{} |
| 327 | case base.AttributeType_ATTRIBUTE_TYPE_BOOLEAN: |
| 328 | target = &base.BooleanValue{} |
| 329 | case base.AttributeType_ATTRIBUTE_TYPE_BOOLEAN_ARRAY: |
| 330 | target = &base.BooleanArrayValue{} |
| 331 | default: |
| 332 | // If attributeType doesn't match any of the known types, return an error indicating invalid argument. |
| 333 | return errors.New(base.ErrorCode_ERROR_CODE_INVALID_ARGUMENT.String()) |
| 334 | } |
| 335 | |
| 336 | // Attempt to unmarshal the value in 'any' into 'target'. |
| 337 | // If this fails, return the error from UnmarshalTo. |
| 338 | if err := any.UnmarshalTo(target); err != nil { |
| 339 | return err |
| 340 | } |
| 341 | |
| 342 | // If the value was successfully unmarshalled and is of the expected type, return nil to indicate success. |
| 343 | return nil |
| 344 | } |
no test coverage detected