func ParseDSN()

in sqldriver/dsn.go [42:136]


func ParseDSN(dsn string) (*Config, error) {
	u, err := url.Parse(dsn)
	if err != nil {
		return nil, err
	}

	accessId := u.User.Username()
	if accessId == "" {
		accessId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")

		if accessId == "" {
			return nil, errors.New("AccessId is not set")
		}
	}

	accessKey, _ := u.User.Password()
	if accessKey == "" {
		accessKey = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

		if accessKey == "" {
			return nil, errors.New("AccessKey is not set")
		}
	}

	queryParams := u.Query()

	projectName := queryParams.Get("project")
	if projectName == "" {
		return nil, errors.New("project name is not set")
	}
	queryParams.Del("project")

	endpoint := (&url.URL{
		Scheme: u.Scheme,
		Host:   u.Host,
		Path:   u.Path,
	}).String()

	config := NewConfig()
	config.AccessId = accessId
	config.AccessKey = accessKey
	config.Endpoint = endpoint
	config.ProjectName = projectName

	var connTimeout, httpTimeout string

	optionalParams := []string{"stsToken", "tcpConnectionTimeout", "httpTimeout", "tunnelEndpoint", "tunnelQuotaName"}
	paramPointer := []*string{&config.StsToken, &connTimeout, &httpTimeout, &config.TunnelEndpoint, &config.TunnelQuotaName}
	for i, p := range optionalParams {
		v := queryParams.Get(p)
		if v != "" {
			*paramPointer[i] = v
		}
		queryParams.Del(p)
	}

	if config.StsToken == "" {
		stsTokenFromEnv := os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN")
		if stsTokenFromEnv != "" {
			config.StsToken = stsTokenFromEnv
		}
	}

	if connTimeout != "" {
		n, err := strconv.ParseInt(connTimeout, 10, 32)
		if err == nil {
			config.TcpConnectionTimeout = time.Duration(n) * time.Second
		}
	}

	if httpTimeout != "" {
		n, err := strconv.ParseInt(httpTimeout, 10, 32)
		if err == nil {
			config.HttpTimeout = time.Duration(n) * time.Second
		}
	}

	otherParams := []string{"enableLogview"}
	config.Others = make(map[string]string)
	for _, p := range otherParams {
		if v := queryParams.Get(p); v != "" {
			config.Others[p] = v
			queryParams.Del(p)
		}
	}

	config.Hints = make(map[string]string)
	if len(queryParams) > 0 {
		for k, params := range queryParams {
			config.Hints[k] = params[0]
		}
	}

	return config, nil
}