| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package openurl provides helpers for URLMux and URLOpeners in portable APIs. |
| 16 |
package openurl // import "gocloud.dev/internal/openurl" |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"net/url" |
| 21 |
"sort" |
| 22 |
"strings" |
| 23 |
) |
| 24 |
|
| 25 |
// SchemeMap maps URL schemes to values. The zero value is an empty map, ready for use. |
| 26 |
// All schemes are stored and compared case-insensitively. |
| 27 |
type SchemeMap struct { |
| 28 |
api string |
| 29 |
m map[string]any |
| 30 |
} |
| 31 |
|
| 32 |
// Register registers scheme for value; subsequent calls to FromString or |
| 33 |
// FromURL with scheme will return value. |
| 34 |
// api is the portable API name (e.g., "blob"); the same value should always |
| 35 |
// be passed. It should be in all lowercase. |
| 36 |
// typ is the portable type (e.g., "Bucket"). |
| 37 |
// Register panics if scheme has already been registered. |
| 38 |
func (m *SchemeMap) Register(api, typ, scheme string, value any) { |
| 39 |
if m.m == nil { |
| 40 |
m.m = map[string]any{} |
| 41 |
} |
| 42 |
if api != strings.ToLower(api) { |
| 43 |
panic(fmt.Errorf("api should be lowercase: %q", api)) |
| 44 |
} |
| 45 |
if m.api == "" { |
| 46 |
m.api = api |
| 47 |
} else if m.api != api { |
| 48 |
panic(fmt.Errorf("previously registered using api %q (now %q)", m.api, api)) |
| 49 |
} |
| 50 |
scheme = strings.ToLower(scheme) |
| 51 |
if _, exists := m.m[scheme]; exists { |
| 52 |
panic(fmt.Errorf("scheme %q already registered for %s.%s", scheme, api, typ)) |
| 53 |
} |
| 54 |
m.m[scheme] = value |
| 55 |
} |
| 56 |
|
| 57 |
// FromString parses urlstr as an URL and looks up the value for the URL's scheme. |
| 58 |
func (m *SchemeMap) FromString(typ, urlstr string) (any, *url.URL, error) { |
| 59 |
u, err := url.Parse(urlstr) |
| 60 |
if err != nil { |
| 61 |
return nil, nil, fmt.Errorf("open %s.%s: %v", m.api, typ, err) |
| 62 |
} |
| 63 |
val, err := m.FromURL(typ, u) |
| 64 |
if err != nil { |
| 65 |
return nil, nil, err |
| 66 |
} |
| 67 |
return val, u, nil |
| 68 |
} |
| 69 |
|
| 70 |
// FromURL looks up the value for u's scheme. |
| 71 |
func (m *SchemeMap) FromURL(typ string, u *url.URL) (any, error) { |
| 72 |
scheme := strings.ToLower(u.Scheme) |
| 73 |
if scheme == "" { |
| 74 |
return nil, fmt.Errorf("open %s.%s: no scheme in URL %q", m.api, typ, u) |
| 75 |
} |
| 76 |
for _, prefix := range []string{ |
| 77 |
fmt.Sprintf("%s+%s+", m.api, strings.ToLower(typ)), |
| 78 |
fmt.Sprintf("%s+", m.api), |
| 79 |
} { |
| 80 |
scheme = strings.TrimPrefix(scheme, prefix) |
| 81 |
} |
| 82 |
v, ok := m.m[scheme] |
| 83 |
if !ok { |
| 84 |
return nil, fmt.Errorf("open %s.%s: no driver registered for %q for URL %q; available schemes: %v", m.api, typ, scheme, u, strings.Join(m.Schemes(), ", ")) |
| 85 |
} |
| 86 |
return v, nil |
| 87 |
} |
| 88 |
|
| 89 |
// Schemes returns a sorted slice of the registered schemes. |
| 90 |
func (m *SchemeMap) Schemes() []string { |
| 91 |
var schemes []string |
| 92 |
for s := range m.m { |
| 93 |
schemes = append(schemes, s) |
| 94 |
} |
| 95 |
sort.Strings(schemes) |
| 96 |
return schemes |
| 97 |
} |
| 98 |
|
| 99 |
// ValidScheme returns true iff scheme has been registered. |
| 100 |
func (m *SchemeMap) ValidScheme(scheme string) bool { |
| 101 |
_, exists := m.m[strings.ToLower(scheme)] |
| 102 |
return exists |
| 103 |
} |