MCPcopy
hub / github.com/go-sql-driver/mysql / normalize

Method normalize

dsn.go:172–234  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

170}
171
172func (cfg *Config) normalize() error {
173 if cfg.InterpolateParams && cfg.Collation != "" && unsafeCollations[cfg.Collation] {
174 return errInvalidDSNUnsafeCollation
175 }
176
177 // Set default network if empty
178 if cfg.Net == "" {
179 cfg.Net = "tcp"
180 }
181
182 // Set default address if empty
183 if cfg.Addr == "" {
184 switch cfg.Net {
185 case "tcp":
186 cfg.Addr = "127.0.0.1:3306"
187 case "unix":
188 cfg.Addr = "/tmp/mysql.sock"
189 default:
190 return errors.New("default addr for network '" + cfg.Net + "' unknown")
191 }
192 } else if cfg.Net == "tcp" {
193 cfg.Addr = ensureHavePort(cfg.Addr)
194 }
195
196 if cfg.TLS == nil {
197 switch cfg.TLSConfig {
198 case "false", "":
199 // don't set anything
200 case "true":
201 cfg.TLS = &tls.Config{}
202 case "skip-verify":
203 cfg.TLS = &tls.Config{InsecureSkipVerify: true}
204 case "preferred":
205 cfg.TLS = &tls.Config{InsecureSkipVerify: true}
206 cfg.AllowFallbackToPlaintext = true
207 default:
208 cfg.TLS = getTLSConfigClone(cfg.TLSConfig)
209 if cfg.TLS == nil {
210 return errors.New("invalid value / unknown config name: " + cfg.TLSConfig)
211 }
212 }
213 }
214
215 if cfg.TLS != nil && cfg.TLS.ServerName == "" && !cfg.TLS.InsecureSkipVerify {
216 host, _, err := net.SplitHostPort(cfg.Addr)
217 if err == nil {
218 cfg.TLS.ServerName = host
219 }
220 }
221
222 if cfg.ServerPubKey != "" {
223 cfg.pubKey = getServerPubKey(cfg.ServerPubKey)
224 if cfg.pubKey == nil {
225 return errors.New("invalid value / unknown server pub key name: " + cfg.ServerPubKey)
226 }
227 }
228
229 if cfg.Logger == nil {

Callers 3

TestNormalizeTLSConfigFunction · 0.95
ParseDSNFunction · 0.80
NewConnectorFunction · 0.80

Calls 3

ensureHavePortFunction · 0.85
getTLSConfigCloneFunction · 0.85
getServerPubKeyFunction · 0.85

Tested by 1

TestNormalizeTLSConfigFunction · 0.76