-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.go
86 lines (75 loc) · 1.91 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package firebase
import (
"errors"
"fmt"
"strings"
"sync"
)
const (
defaultAppName = "[DEFAULT]"
)
var apps = struct {
sync.Mutex
m map[string]*App
}{
m: make(map[string]*App),
}
// App is the entry point of the SDK. It holds common configuration and state
// for Firebase APIs. Most applications don't need to directly interact with
// App.
type App struct {
name string
options *Options
}
// GetApp retrieves the default instance of the App, creating it if necessary.
func GetApp() (*App, error) {
return GetAppWithName(defaultAppName)
}
// GetAppWithName retrieves an instance of the App with a given name, creating it if necessary.
func GetAppWithName(name string) (*App, error) {
apps.Lock()
defer apps.Unlock()
name = normalize(name)
if app, ok := apps.m[name]; ok {
return app, nil
}
return nil, fmt.Errorf("App with name %s not yet initialized!", name)
}
// Name returns the name of the App.
func (app *App) Name() string {
return app.name
}
func normalize(name string) string {
return strings.TrimSpace(name)
}
func (app *App) isDefaultApp() bool {
return app.name == defaultAppName
}
// InitializeApp initializes the default App instance.
func InitializeApp(o *Options) (*App, error) {
return InitializeAppWithName(o, defaultAppName)
}
// InitializeAppWithName initializes an App with a unique given name.
//
// It is an error to initialize an app with an already existing name. Starting
// and ending whitespace characters in the name are ignored (trimmed).
func InitializeAppWithName(o *Options, name string) (*App, error) {
name = normalize(name)
if name == "" {
return nil, errors.New("App name cannot be empty")
}
if o == nil {
return nil, errors.New("Options cannot be nil")
}
apps.Lock()
defer apps.Unlock()
if _, ok := apps.m[name]; ok {
return nil, fmt.Errorf("App name %s already exists!", name)
}
app := &App{
name: name,
options: o,
}
apps.m[name] = app
return app, nil
}