-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
38 lines (32 loc) · 866 Bytes
/
user.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
package sample_circleci
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
type User struct {
UserID string `dynamodbav:"user_id"`
UserName string `dynamodbav:"user_name"`
}
func FetchUserByID(ctx context.Context, userID string) (*User, error) {
q := &dynamodb.GetItemInput{
TableName: aws.String(userTable),
Key: map[string]*dynamodb.AttributeValue{
"user_id": {
S: aws.String(userID),
},
},
ConsistentRead: aws.Bool(true),
}
out, err := db.GetItemWithContext(ctx, q)
if err != nil {
return nil, fmt.Errorf("dynamodb fetch user: %w", err)
}
var user *User
if err := dynamodbattribute.UnmarshalMap(out.Item, &user); err != nil {
return nil, fmt.Errorf("dynamodb unmarshal: %w", err)
}
return user, nil
}