-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
136 lines (107 loc) · 3.13 KB
/
main.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
package main
import (
"encoding/json"
"facebook-business-sdk-codegen-relationships/database"
"facebook-business-sdk-codegen-relationships/models"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"slices"
"strings"
)
var (
CodegenRepository = "https://github.com/facebook/facebook-business-sdk-codegen"
CodegenVersionFile = "/api_specs/specs/version.txt"
CodegenLocalPath = "sdk"
ExcludedFiles = []string{"version.txt", "enum_types.json"}
)
func PullCodegen() error {
// verify if git is installed
_, err := exec.LookPath("git")
if err != nil {
log.Fatal("git is not installed")
}
cmd := exec.Command("git", "clone", CodegenRepository, CodegenLocalPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func main() {
fmt.Println("\n=============================================================")
fmt.Println(" 🚀 STARTING - CODEGEN RELATIONSHIPS IMPORTER ")
fmt.Println("=============================================================\n")
// Init Neo4j Driver
database.InitNeo4j()
// Reset All AdObjects & Fields
err := database.ResetDatabase()
if err != nil {
log.Fatal(err)
}
// check if the sdk is already downloaded
if _, err := os.Stat(CodegenLocalPath); os.IsNotExist(err) {
errP := PullCodegen()
if errP != nil {
log.Fatal(errP)
}
}
// check if the sdk is the sdk version in sdk/api_specs/specs/version.txt
versionFile, errV := os.ReadFile(CodegenLocalPath + CodegenVersionFile)
if errV != nil {
log.Fatal(errV)
}
version := strings.Trim(string(versionFile), "\n")
fmt.Println("Actual SDK version: " + version + "\n")
// Read all json files in the api_specs/specs folder
fileNames, err := os.ReadDir(CodegenLocalPath + "/api_specs/specs")
if err != nil {
log.Fatal(err)
}
parsed := make(map[string]models.AdObject)
for _, name := range fileNames {
// skip the excluded files
if slices.Contains(ExcludedFiles, name.Name()) {
continue
}
file, errJ := os.ReadFile(CodegenLocalPath + "/api_specs/specs/" + name.Name())
if errJ != nil {
log.Fatal(errJ)
}
var result models.AdObject
err = json.Unmarshal(file, &result)
parsed[strings.Replace(name.Name(), ".json", "", -1)] = result
}
// First Create all AdObjects with their fields
for name, obj := range parsed {
err := database.CreateAdObject(name, obj.Fields)
if err != nil {
log.Fatal(err)
}
}
// Then Create all Field Relationships
for r, fields := range parsed {
foundRelationship := false
for _, field := range fields.Fields {
toCheck := field.Type
// example Type: AdAssetFeedSpecTitle or list<AdAssetFeedSpecTitle>
re := regexp.MustCompile(`list<([^>]+)>`)
match := re.FindAllStringSubmatch(toCheck, -1)
if len(match) > 0 {
toCheck = match[0][1]
}
if len(parsed[toCheck].Fields) > 0 {
foundRelationship = true
fmt.Println("🔥 Found relationship " + r + " ( " + field.Name + " ) <-> " + toCheck + "")
err := database.CreateLinkBetweenFieldAndObject(toCheck)
if err != nil {
log.Fatal(err)
}
}
}
if foundRelationship {
fmt.Println("\n--------------------------------------\n")
}
}
fmt.Println("\n✅ Importation Done")
}