-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessor.go
141 lines (128 loc) · 3.94 KB
/
accessor.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package adstorage
import (
"context"
"github.com/geniusrabbit/adcorelib/accesspoint"
"github.com/geniusrabbit/adcorelib/admodels/types"
"github.com/geniusrabbit/adstorage/accessors/accesspointaccessor"
"github.com/geniusrabbit/adstorage/accessors/accountaccessor"
"github.com/geniusrabbit/adstorage/accessors/adsourceaccessor"
"github.com/geniusrabbit/adstorage/accessors/appaccessor"
"github.com/geniusrabbit/adstorage/accessors/campaignaccessor"
"github.com/geniusrabbit/adstorage/accessors/formataccessor"
"github.com/geniusrabbit/adstorage/accessors/zoneaccessor"
)
type AllAccessor[AccType any] struct {
ctx context.Context
accessors AllDataAccessor[AccType]
formats types.FormatsAccessor
accountCast accountaccessor.AccountConvertFunc[AccType]
accounts *accountaccessor.AccountAccessor[AccType]
apps *appaccessor.AppAccessor
zones *zoneaccessor.ZoneAccessor
campaigns *campaignaccessor.CampaignAccessor
}
func NewAllAccessor[AccType any](ctx context.Context, accessors AllDataAccessor[AccType], accountCast accountaccessor.AccountConvertFunc[AccType]) *AllAccessor[AccType] {
return &AllAccessor[AccType]{
ctx: ctx,
accessors: accessors,
accountCast: accountCast,
}
}
func (acc *AllAccessor[AccType]) Formats() (types.FormatsAccessor, error) {
if acc.formats != nil {
return acc.formats, nil
}
formatDataAccessor, err := acc.accessors.Formats()
if err != nil {
return nil, err
}
acc.formats = formataccessor.NewFormatAccessor(formatDataAccessor)
return acc.formats, nil
}
func (acc *AllAccessor[AccType]) Accounts() (*accountaccessor.AccountAccessor[AccType], error) {
if acc.accounts != nil {
return acc.accounts, nil
}
accountDataAccessor, err := acc.accessors.Accounts()
if err != nil {
return nil, err
}
acc.accounts = accountaccessor.NewAccessor(accountDataAccessor, acc.accountCast)
return acc.accounts, nil
}
func (acc *AllAccessor[AccType]) Apps() (*appaccessor.AppAccessor, error) {
if acc.apps != nil {
return acc.apps, nil
}
accounts, err := acc.Accounts()
if err != nil {
return nil, err
}
appDataAccessor, err := acc.accessors.Apps()
if err != nil {
return nil, err
}
acc.apps = appaccessor.NewAppAccessor(appDataAccessor, accounts)
return acc.apps, nil
}
func (acc *AllAccessor[AccType]) Zones() (*zoneaccessor.ZoneAccessor, error) {
if acc.zones != nil {
return acc.zones, nil
}
accounts, err := acc.Accounts()
if err != nil {
return nil, err
}
zoneDataAccessor, err := acc.accessors.Zones()
if err != nil {
return nil, err
}
acc.zones = zoneaccessor.NewZoneAccessor(zoneDataAccessor, accounts)
return acc.zones, nil
}
func (acc *AllAccessor[AccType]) Campaigns(prepareFunc campaignaccessor.CampaignPrepareFunc) (*campaignaccessor.CampaignAccessor, error) {
if acc.campaigns != nil {
return acc.campaigns, nil
}
formats, err := acc.Formats()
if err != nil {
return nil, err
}
accounts, err := acc.Accounts()
if err != nil {
return nil, err
}
campaignDataAccessor, err := acc.accessors.Campaigns()
if err != nil {
return nil, err
}
acc.campaigns = campaignaccessor.NewCampaignAccessor(campaignDataAccessor, accounts, formats, prepareFunc)
return acc.campaigns, nil
}
func (acc *AllAccessor[AccType]) Sources(factories ...adsourceaccessor.SourceFactory) (*adsourceaccessor.Accessor[AccType], error) {
accounts, err := acc.Accounts()
if err != nil {
return nil, err
}
sourceDataAccessor, err := acc.accessors.RTBSources()
if err != nil {
return nil, err
}
return adsourceaccessor.NewAccessor(acc.ctx, sourceDataAccessor, accounts, factories...)
}
func (acc *AllAccessor[AccType]) AccessPoints(factoryList ...accesspoint.Factory) (*accesspointaccessor.Accessor, error) {
accounts, err := acc.Accounts()
if err != nil {
return nil, err
}
accesspointDataAccessor, err := acc.accessors.RTBAccessPoints()
if err != nil {
return nil, err
}
return accesspointaccessor.NewAccessor(
acc.ctx,
accesspointDataAccessor,
accounts,
factoryList...,
)
}