Skip to content

Commit 3ed39bd

Browse files
committed
Up personification module structure
1 parent cb61b5d commit 3ed39bd

11 files changed

Lines changed: 198 additions & 156 deletions

File tree

context/ctxperson/person.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55

66
"github.com/geniusrabbit/adcorelib/personification"
7+
"github.com/geniusrabbit/adcorelib/personification/dummy"
78
)
89

910
var (
1011
// CtxPersonObject reference to the person accessor
1112
CtxPersonObject = struct{ s string }{"person"}
12-
dummyClient personification.DummyClient
13+
dummyClient dummy.DummyClient
1314
)
1415

1516
// Get logger object

context/ctxperson/person_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/geniusrabbit/adcorelib/personification"
7+
"github.com/geniusrabbit/adcorelib/personification/dummy"
88

99
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func TestLogger(t *testing.T) {
1313
ctx := context.Background()
14-
ctx = WithPersonClient(ctx, &personification.DummyClient{})
14+
ctx = WithPersonClient(ctx, &dummy.DummyClient{})
1515
assert.NotNil(t, Get(ctx))
1616
}

personification/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import (
66
"github.com/geniusrabbit/udetect"
77
)
88

9+
type (
10+
Request = udetect.Request
11+
Response = udetect.Response
12+
)
13+
914
// Client interface
1015
type Client interface {
11-
Detect(ctx context.Context, req *udetect.Request) (*udetect.Response, error)
16+
Detect(ctx context.Context, req *Request) (*Response, error)
1217
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package personification
1+
package dummy
22

33
import (
44
"context"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package personification
1+
package dummy
22

33
import "github.com/google/uuid"
44

55
func isEmptyUUIDPtr(uuid *uuid.UUID) bool {
66
if uuid != nil {
7-
for i := 0; i < len(*uuid); i++ {
7+
for i := range len(*uuid) {
88
if (*uuid)[i] != 0 {
99
return false
1010
}

personification/iface.go

Lines changed: 14 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
package personification
22

3-
import (
4-
"github.com/geniusrabbit/udetect"
5-
)
6-
73
type (
8-
// UserInfo value
9-
UserInfo struct {
10-
User *udetect.User
11-
Device *udetect.Device
12-
Geo *udetect.Geo
13-
}
14-
154
// PredictRequest ...
165
PredictRequest struct{}
176

@@ -25,89 +14,25 @@ type (
2514
PredictPriceResponse struct{}
2615
)
2716

28-
// UUID of the user
29-
func (i *UserInfo) UUID() string {
30-
if i == nil || i.User == nil || isEmptyUUIDPtr(&i.User.UUID) {
31-
return ""
32-
}
33-
return i.User.UUID.String()
34-
}
35-
36-
// SessionID of the user
37-
func (i *UserInfo) SessionID() string {
38-
if i == nil || i.User == nil {
39-
return ""
40-
}
41-
return i.User.SessionID
42-
}
43-
44-
// Fingerprint of the iser
45-
func (i *UserInfo) Fingerprint() string {
46-
if i == nil || i.User == nil {
47-
return ""
48-
}
49-
return i.User.FingerPrintID
50-
}
51-
52-
// Country info
53-
func (i *UserInfo) Country() *udetect.Geo {
54-
if i == nil || i.Geo == nil {
55-
return &udetect.GeoDefault
56-
}
57-
return i.Geo
58-
}
59-
60-
// Ages of the user
61-
func (i *UserInfo) Ages() (from, to int) {
62-
if i == nil || i.User == nil {
63-
return 0, 0
64-
}
65-
return i.User.AgeStart, i.User.AgeEnd
66-
}
67-
68-
// ETag of the user
69-
func (i *UserInfo) ETag() string {
70-
if i == nil || i.User == nil {
71-
return ""
72-
}
73-
return i.User.ETag
74-
}
17+
// Properties accessor
18+
type Properties interface {
19+
// Get property by key
20+
Get(key string) any
7521

76-
// Keywords of the user
77-
func (i *UserInfo) Keywords() string {
78-
if i == nil || i.User == nil {
79-
return ""
80-
}
81-
return i.User.Keywords
82-
}
22+
// GetString property by key
23+
GetString(key string) string
8324

84-
// MostPossibleSex of the user
85-
func (i *UserInfo) MostPossibleSex() int {
86-
if i == nil || i.User == nil {
87-
return 0
88-
}
89-
return i.User.MostPossibleSex()
90-
}
25+
// GetIntSlice property by key
26+
GetIntSlice(key string) []int
9127

92-
// DeviceInfo get method
93-
func (i *UserInfo) DeviceInfo() *udetect.Device {
94-
if i == nil || i.Device == nil {
95-
return &udetect.DeviceDefault
96-
}
97-
return i.Device
98-
}
28+
// Set property
29+
Set(key string, prop any)
9930

100-
// GeoInfo get method
101-
func (i *UserInfo) GeoInfo() *udetect.Geo {
102-
if i == nil || i.Geo == nil {
103-
return &udetect.GeoDefault
104-
}
105-
return i.Geo
106-
}
31+
// Delete property by key
32+
Delete(key string)
10733

108-
// GeoInfo get method
109-
func (i *UserInfo) CarrierInfo() *udetect.Carrier {
110-
return i.GeoInfo().Carrier
34+
// Synchronise properties
35+
Synchronise() error
11136
}
11237

11338
// Person information block
@@ -127,24 +52,3 @@ type Person interface {
12752
// PredictPrice what minimal
12853
PredictPrice(req *PredictPriceRequest) (*PredictPriceResponse, error)
12954
}
130-
131-
// Properties accessor
132-
type Properties interface {
133-
// Get property by key
134-
Get(key string) any
135-
136-
// GetString property by key
137-
GetString(key string) string
138-
139-
// GetIntSlice property by key
140-
GetIntSlice(key string) []int
141-
142-
// Set property
143-
Set(key string, prop any)
144-
145-
// Delete property by key
146-
Delete(key string)
147-
148-
// Synchronise properties
149-
Synchronise() error
150-
}

personification/person.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
type person struct {
8-
request *udetect.Request
8+
request *Request
99
userInfo UserInfo
1010
}
1111

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package personification
1+
package simple
22

33
import (
44
"context"
55
"strings"
66

77
"github.com/google/uuid"
88
useragent "github.com/mileusna/useragent"
9-
"golang.org/x/exp/constraints"
109

1110
"github.com/geniusrabbit/udetect"
1211
)
@@ -16,6 +15,8 @@ type Item struct {
1615
Name string
1716
}
1817

18+
// SimpleClient represents a simple implementation of the Client interface
19+
// that uses the mileusna/useragent package for user-agent parsing.
1920
type SimpleClient struct {
2021
BrowserList []*Item
2122
OSList []*Item
@@ -79,38 +80,3 @@ func (s *SimpleClient) osGet(name string) uint {
7980
}
8081
return 0
8182
}
82-
83-
func deviceType(ua *useragent.UserAgent) udetect.DeviceType {
84-
if ua.Mobile {
85-
return udetect.DeviceTypeMobile
86-
}
87-
if ua.Tablet {
88-
return udetect.DeviceTypeTablet
89-
}
90-
if ua.Desktop {
91-
return udetect.DeviceTypePC
92-
}
93-
if strings.Contains(ua.Name, "AppleTV") {
94-
return udetect.DeviceTypeTV
95-
}
96-
if strings.Contains(ua.String, "PlayStation") || strings.Contains(ua.String, "Xbox") {
97-
return udetect.DeviceTypeSetTopBox
98-
}
99-
return udetect.DeviceTypeUnknown
100-
}
101-
102-
func isEmpty(uid uuid.UUID) bool {
103-
for i := range len(uid) {
104-
if uid[i] != 0 {
105-
return false
106-
}
107-
}
108-
return true
109-
}
110-
111-
func b2i[R constraints.Integer | constraints.Float](b bool) R {
112-
if b {
113-
return 1
114-
}
115-
return 0
116-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package personification
1+
package simple
22

33
import (
44
"encoding/json"

personification/simple/utils.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package simple
2+
3+
import (
4+
"strings"
5+
6+
"github.com/geniusrabbit/udetect"
7+
"github.com/google/uuid"
8+
useragent "github.com/mileusna/useragent"
9+
"golang.org/x/exp/constraints"
10+
)
11+
12+
func deviceType(ua *useragent.UserAgent) udetect.DeviceType {
13+
if ua.Mobile {
14+
return udetect.DeviceTypeMobile
15+
}
16+
if ua.Tablet {
17+
return udetect.DeviceTypeTablet
18+
}
19+
if ua.Desktop {
20+
return udetect.DeviceTypePC
21+
}
22+
if strings.Contains(ua.Name, "AppleTV") {
23+
return udetect.DeviceTypeTV
24+
}
25+
if strings.Contains(ua.String, "PlayStation") || strings.Contains(ua.String, "Xbox") {
26+
return udetect.DeviceTypeSetTopBox
27+
}
28+
return udetect.DeviceTypeUnknown
29+
}
30+
31+
func isEmpty(uid uuid.UUID) bool {
32+
for i := range len(uid) {
33+
if uid[i] != 0 {
34+
return false
35+
}
36+
}
37+
return true
38+
}
39+
40+
func b2i[R constraints.Integer | constraints.Float](b bool) R {
41+
if b {
42+
return 1
43+
}
44+
return 0
45+
}

0 commit comments

Comments
 (0)