-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator_field.go
114 lines (98 loc) · 3.04 KB
/
translator_field.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
package main
import (
"fmt"
"net/http"
"regexp"
"strings"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
)
// convertSanityPathToGJSONPath converts Sanity paths to gjson compatible paths
func convertSanityPathToGJSONPath(sanityPath string) string {
// Convert array accessors from [index] to .index
re := regexp.MustCompile(`\[(\d+)\]`)
return re.ReplaceAllString(sanityPath, ".$1")
}
// SanityTranslateField handles the main logic for translating a specific field in Sanity documents.
func SanityTranslateField(c *gin.Context) {
var txx SanityFieldTranslator
fmt.Println("Translating field")
// Create a Translator object adding all the info from the request
if err := c.BindJSON(&txx); err != nil {
c.String(http.StatusBadRequest, "Failed binding event to JSON")
fmt.Println("Failed binding event to JSON")
return
}
// Create a SanityDocument object adding all the info from Sanity API
query := fmt.Sprintf(`*[slug.current == '%s'][0]`, txx.FromSlug)
originalDocument, err := RunQuery(query)
if err != nil || originalDocument == "" {
c.String(http.StatusBadRequest, "Error extracting original_doc from Sanity")
fmt.Println("Error extracting original_doc from Sanity")
return
}
result := gjson.Get(originalDocument, "result").Raw
txx.Id = gjson.Get(result, "_id").String()
txx.Before = result
for _, mappingField := range txx.MappingFields {
gjsonPath := convertSanityPathToGJSONPath(mappingField.SanityPath)
fieldValue := gjson.Get(txx.Before, gjsonPath).String()
if fieldValue == "" {
c.String(http.StatusBadRequest, "Field not found in the document")
fmt.Println("Field not found in the document")
return
}
for _, toSlug := range txx.ToSlugs {
query = fmt.Sprintf(`*[slug.current == '%s'][0]`, toSlug)
translatedDoc, err := RunQuery(query)
if err != nil {
c.String(http.StatusBadRequest, "Error extracting translated_doc from Sanity")
fmt.Println("Error extracting translated_doc from Sanity")
return
}
translatedDocResult := gjson.Get(translatedDoc, "result").Raw
translatedDocID := gjson.Get(translatedDocResult, "_id").String()
translatedToLang := toSlug[1:3]
translatedValue, err := RunDeepl(fieldValue, txx.FromLang, translatedToLang)
if err != nil {
c.String(http.StatusBadRequest, "Failed executing translation")
fmt.Println("Failed executing translation")
return
}
translatedValue = strings.TrimSpace(translatedValue)
rawPatch := fmt.Sprintf(`
{
"mutations": [
{
"patch": {
"id": "%s",
"set": {
"%s": "%s"
}
}
}
]
}`,
translatedDocID,
mappingField.SanityPath,
translatedValue,
)
err = RunMutation(rawPatch)
if err != nil {
errorMsg := fmt.Sprintf("Failed patching translated field: %v", err)
c.String(http.StatusBadRequest, errorMsg)
fmt.Println(errorMsg)
return
}
fmt.Printf("\tTranslating field: %s\n", toSlug)
}
fmt.Println("")
}
c.JSON(
http.StatusOK,
gin.H{
"status": "success",
"message": "Field translation completed",
},
)
}