<commit_before>package client

import (
	"fmt"
	"github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http"
	"github.com/cloudevents/sdk-go/pkg/cloudevents/transport/nats"
	nethttp "net/http"
	"net/url"
)

type ClientOption func(*Client) error

// WithTarget sets the outbound recipient of cloudevents when using a http request.
func WithTarget(targetUrl string) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			if targetUrl != "" {
				var err error
				var target *url.URL
				target, err = url.Parse(targetUrl)
				if err != nil {
					return fmt.Errorf("client option failed to parse target url: %s", err.Error())
				}

				if t.Req == nil {
					t.Req = &nethttp.Request{
						Method: nethttp.MethodPost,
					}
				}
				t.Req.URL = target
				return nil
			} else {
				return fmt.Errorf("target option was empty string")
			}
		}
		return fmt.Errorf("invalid client option recieved for given transport type")
	}
}

// WithTarget sets the outbound recipient of cloudevents when using a http request.
func WithHTTPMethod(method string) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			if method != "" {
				if t.Req == nil {
					t.Req = &nethttp.Request{}
				}
				t.Req.Method = method
				return nil
			} else {
				return fmt.Errorf("context client option was nil")
			}
		}
		return fmt.Errorf("invalid client option recieved for given transport type")
	}
}

// WithHTTPEncoding sets the encoding for clients with HTTP transports.
func WithHTTPEncoding(encoding http.Encoding) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			t.Encoding = encoding
			return nil
		}
		return fmt.Errorf("invalid client option recieved for given client type")
	}
}

// WithHTTPPort sets the port for accepting requests using HTTP transport.
func WithHTTPPort(port int) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			if port == 0 {
				return fmt.Errorf("client option was given an invalid port: %d", port)
			}
			t.Port = port
			return nil
		}
		return fmt.Errorf("invalid client option recieved for given client type")
	}
}

// WithNATSEncoding sets the encoding for clients with NATS transport.
func WithNATSEncoding(encoding nats.Encoding) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*nats.Transport); ok {
			t.Encoding = encoding
			return nil
		}
		return fmt.Errorf("invalid client option recieved for given client type")
	}
}<commit_msg>make client option errors easy to trace back to the issue.<commit_after>package client

import (
	"fmt"
	"github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http"
	"github.com/cloudevents/sdk-go/pkg/cloudevents/transport/nats"
	nethttp "net/http"
	"net/url"
)

type ClientOption func(*Client) error

// WithTarget sets the outbound recipient of cloudevents when using an HTTP request.
func WithTarget(targetUrl string) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			if targetUrl != "" {
				var err error
				var target *url.URL
				target, err = url.Parse(targetUrl)
				if err != nil {
					return fmt.Errorf("client option failed to parse target url: %s", err.Error())
				}

				if t.Req == nil {
					t.Req = &nethttp.Request{
						Method: nethttp.MethodPost,
					}
				}
				t.Req.URL = target
				return nil
			} else {
				return fmt.Errorf("target option was empty string")
			}
		}
		return fmt.Errorf("invalid target client option recieved for transport type")
	}
}

// WithHTTPMethod sets the outbound recipient of cloudevents when using an HTTP request.
func WithHTTPMethod(method string) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			if method != "" {
				if t.Req == nil {
					t.Req = &nethttp.Request{}
				}
				t.Req.Method = method
				return nil
			} else {
				return fmt.Errorf("context client option was nil")
			}
		}
		return fmt.Errorf("invalid HTTP method client option recieved for transport type")
	}
}

// WithHTTPEncoding sets the encoding for clients with HTTP transports.
func WithHTTPEncoding(encoding http.Encoding) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			t.Encoding = encoding
			return nil
		}
		return fmt.Errorf("invalid HTTP encoding client option recieved for transport type")
	}
}

// WithHTTPPort sets the port for for clients with HTTP transports.
func WithHTTPPort(port int) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*http.Transport); ok {
			if port == 0 {
				return fmt.Errorf("client option was given an invalid port: %d", port)
			}
			t.Port = port
			return nil
		}
		return fmt.Errorf("invalid HTTP port client option recieved for transport type")
	}
}

// WithNATSEncoding sets the encoding for clients with NATS transport.
func WithNATSEncoding(encoding nats.Encoding) ClientOption {
	return func(c *Client) error {
		if t, ok := c.transport.(*nats.Transport); ok {
			t.Encoding = encoding
			return nil
		}
		return fmt.Errorf("invalid NATS encoding client option recieved for transport type")
	}
}
{'fraction_non_alphanumeric': 0.10697940503432495, 'fraction_numerical': 0.00038138825324180017, 'mean_word_length': 3.3490878938640134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1059452', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1640, 'n_words': 534}