( ctx context.Context, schemaImage bufimage.Image, messageInput string, typeName string, defaultMessageEncoding buffetch.MessageEncoding, options ...FunctionOption, )
| 721 | } |
| 722 | |
| 723 | func (c *controller) GetMessage( |
| 724 | ctx context.Context, |
| 725 | schemaImage bufimage.Image, |
| 726 | messageInput string, |
| 727 | typeName string, |
| 728 | defaultMessageEncoding buffetch.MessageEncoding, |
| 729 | options ...FunctionOption, |
| 730 | ) (_ proto.Message, _ buffetch.MessageEncoding, retErr error) { |
| 731 | defer c.handleFileAnnotationSetRetError(&retErr) |
| 732 | functionOptions := newFunctionOptions(c) |
| 733 | for _, option := range options { |
| 734 | option(functionOptions) |
| 735 | } |
| 736 | // Must be messageRefParser NOT c.buffetchRefParser as a NewMessageRefParser |
| 737 | // defaults to a defaultMessageEncoding and not dir. |
| 738 | messageRefParser := buffetch.NewMessageRefParser( |
| 739 | c.logger, |
| 740 | buffetch.MessageRefParserWithDefaultMessageEncoding( |
| 741 | defaultMessageEncoding, |
| 742 | ), |
| 743 | ) |
| 744 | messageRef, err := messageRefParser.GetMessageRef(ctx, messageInput) |
| 745 | if err != nil { |
| 746 | return nil, 0, err |
| 747 | } |
| 748 | messageEncoding := messageRef.MessageEncoding() |
| 749 | if messageRef.IsNull() { |
| 750 | return nil, messageEncoding, nil |
| 751 | } |
| 752 | var validator protoyaml.Validator |
| 753 | if functionOptions.messageValidation { |
| 754 | protovalidateValidator, err := protovalidate.New( |
| 755 | protovalidate.WithExtensionTypeResolver(schemaImage.Resolver()), |
| 756 | ) |
| 757 | if err != nil { |
| 758 | return nil, 0, err |
| 759 | } |
| 760 | validator = yamlValidator{protovalidateValidator} |
| 761 | } |
| 762 | var unmarshaler protoencoding.Unmarshaler |
| 763 | switch messageEncoding { |
| 764 | case buffetch.MessageEncodingBinpb: |
| 765 | unmarshaler = protoencoding.NewWireUnmarshaler(schemaImage.Resolver()) |
| 766 | case buffetch.MessageEncodingJSON: |
| 767 | unmarshaler = protoencoding.NewJSONUnmarshaler(schemaImage.Resolver()) |
| 768 | case buffetch.MessageEncodingTxtpb: |
| 769 | unmarshaler = protoencoding.NewTxtpbUnmarshaler(schemaImage.Resolver()) |
| 770 | case buffetch.MessageEncodingYAML: |
| 771 | unmarshaler = protoencoding.NewYAMLUnmarshaler( |
| 772 | schemaImage.Resolver(), |
| 773 | protoencoding.YAMLUnmarshalerWithPath(messageRef.Path()), |
| 774 | // This will pretty print validation errors. |
| 775 | protoencoding.YAMLUnmarshalerWithValidator(validator), |
| 776 | ) |
| 777 | validator = nil // Validation errors are handled by the unmarshaler. |
| 778 | default: |
| 779 | // This is a system error. |
| 780 | return nil, 0, syserror.Newf("unknown MessageEncoding: %v", messageEncoding) |
nothing calls this directly
no test coverage detected