-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.go
155 lines (139 loc) · 3.56 KB
/
operations.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package go_oauth2_v3_dynamodb
import (
"encoding/json"
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/models"
)
func insert(item interface{}, tableName string, db *dynamodb.DynamoDB) (err error) {
var av map[string]*dynamodb.AttributeValue
av, err = dynamodbattribute.MarshalMap(item)
if err != nil {
return
}
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(tableName),
}
_, err = db.PutItem(input)
return
}
func remove(id string, tableName string, db *dynamodb.DynamoDB) (err error) {
cId := clientId{ID: id}
var av map[string]*dynamodb.AttributeValue
av, err = dynamodbattribute.MarshalMap(cId)
if err != nil {
return
}
input := &dynamodb.DeleteItemInput{
Key: av,
TableName: aws.String(tableName),
}
_, err = db.DeleteItem(input)
return
}
func getClient(id string, tableName string, db *dynamodb.DynamoDB) (info oauth2.ClientInfo, err error) {
var io *dynamodb.GetItemOutput
io, err = getRaw(id, tableName, db)
if err != nil {
return
}
item := &client{}
err = dynamodbattribute.UnmarshalMap(io.Item, &item)
if err != nil {
return
}
if item.ClientId == "" {
err = errors.New("not found")
return
}
info = &models.Client{
ID: item.ClientId,
Secret: item.ClientSecret,
UserID: item.UserID,
}
return
}
func getRaw(id string, tableName string, db *dynamodb.DynamoDB) (io *dynamodb.GetItemOutput, err error) {
io, err = db.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{
"client_id": {
S: aws.String(id),
},
},
})
if err != nil {
return
}
return
}
func getToStruct(id string, tableName string, db *dynamodb.DynamoDB, strct interface{}) (err error) {
var io *dynamodb.GetItemOutput
io, err = db.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{
"client_id": {
S: aws.String(id),
},
},
})
if err != nil {
return
}
err = dynamodbattribute.UnmarshalMap(io.Item, &strct)
return
}
func getToken(id string, tableName string, db *dynamodb.DynamoDB) (ti oauth2.TokenInfo, err error) {
var io *dynamodb.GetItemOutput
io, err = getRaw(id, tableName, db)
if err != nil {
return
}
var bd basicData
err = dynamodbattribute.UnmarshalMap(io.Item, &bd)
if err != nil {
return
}
if bd.ID == "" {
err = errors.New("not found")
return
}
var tm models.Token
err = json.Unmarshal(bd.Data, &tm)
if err != nil {
return
}
ti = &tm
return
}
func createTable(tableName string, definitions, keySchemas map[string]string, secondIndex *[]*dynamodb.GlobalSecondaryIndex, db *dynamodb.DynamoDB) (err error) {
var dAttributeDefinitions []*dynamodb.AttributeDefinition
var dKeySchemas []*dynamodb.KeySchemaElement
for key, val := range definitions {
dAttributeDefinitions = append(dAttributeDefinitions, &dynamodb.AttributeDefinition{
AttributeName: aws.String(key),
AttributeType: aws.String(val),
})
}
for key, val := range keySchemas {
dKeySchemas = append(dKeySchemas, &dynamodb.KeySchemaElement{
AttributeName: aws.String(key),
KeyType: aws.String(val),
})
}
input := &dynamodb.CreateTableInput{
AttributeDefinitions: dAttributeDefinitions,
KeySchema: dKeySchemas,
BillingMode: aws.String("PAY_PER_REQUEST"),
TableName: aws.String(tableName),
}
if secondIndex != nil {
input.GlobalSecondaryIndexes = *secondIndex
}
_, err = db.CreateTable(input)
return
}