/
serializer.go
51 lines (41 loc) · 1.2 KB
/
serializer.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
)
// WriteUsers serializes a slice of users to a JSON file.
func WriteUsers(file string, users []*cognitoidentityprovider.UserType) int {
json, jsonErr := json.Marshal(users)
if jsonErr != nil {
log.Fatal("Failed to encode the users to JSON: ", jsonErr.Error())
}
err := ioutil.WriteFile(file, json, 0644)
if err != nil {
log.Fatal("Failed to write the users to a file: ", err.Error())
}
return len(users)
}
// ReadUsers deserializes an array of users from a JSON file.
func ReadUsers(file string) []*cognitoidentityprovider.UserType {
raw, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal("Failed to read the JSON file: ", err.Error())
os.Exit(1)
}
var users []*cognitoidentityprovider.UserType
json.Unmarshal(raw, &users)
// We must filter out the "sub" property that is managed by Cognito.
for userIndex, user := range users {
for attrIndex, attr := range user.Attributes {
if *attr.Name == "sub" {
user.Attributes = append(user.Attributes[:attrIndex], user.Attributes[attrIndex+1:]...)
break
}
}
users[userIndex] = user
}
return users
}