func parseHostPort()

in dax/internal/client/cluster.go [432:481]


func parseHostPort(hostPort string) (host string, port int, scheme string, err error) {
	uriString := hostPort
	colon := strings.Index(hostPort, "://")

	handle := func(e error) (host string, port int, scheme string, err error) {
		return "", 0, "", e
	}

	if colon == -1 {
		if strings.Index(hostPort, ":") == -1 {
			return handle(&smithy.GenericAPIError{
				Code:    ErrCodeInvalidParameter,
				Message: fmt.Sprintf(hostPort + "Invalid hostport."),
				Fault:   smithy.FaultClient,
			})
		}
		uriString = "dax://" + hostPort
	}
	u, err := url.ParseRequestURI(uriString)
	if err != nil {
		return handle(err)
	}

	host = u.Hostname()
	scheme = u.Scheme
	portStr := u.Port()
	if host == "" {
		return handle(&smithy.GenericAPIError{
			Code:    ErrCodeInvalidParameter,
			Message: fmt.Sprintf("Invalid hostport"),
			Fault:   smithy.FaultClient,
		})
	}

	port, err = strconv.Atoi(portStr)
	if err != nil {
		port = defaultPorts[scheme]
	}

	if _, ok := defaultPorts[scheme]; !ok {
		schemes := strings.Join(strings.Fields(fmt.Sprint(reflect.ValueOf(defaultPorts).MapKeys())), ",")
		return handle(&smithy.GenericAPIError{
			Code:    ErrCodeInvalidParameter,
			Message: fmt.Sprintf("URL scheme must be one of " + schemes),
			Fault:   smithy.FaultClient,
		})
	}

	return host, port, scheme, nil
}