Skip to content

Commit 787a45a

Browse files
committed
chore: formatting
1 parent 546af18 commit 787a45a

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

cmd/utils.go

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,70 +61,71 @@ func formatSqlCode(sqlxFileMetaData *sqlxParserMeta, pythonScriptPath string, sq
6161
err := cmd.Run()
6262
if err != nil {
6363
logger.Error(stderr.String(), slog.String("file", sqlxFileMetaData.filepath), "error", err.Error())
64-
sqlxFileMetaData.sqlBlocksMeta.formattedSqlBlockContent = string(queryString)
64+
sqlxFileMetaData.sqlBlocksMeta.formattedSqlBlockContent = string(queryString)
6565
return ErrorFormattingSqlxFile
6666
}
6767
output := stdout.String()
6868
sql_fluff_not_installed := (strings.TrimSpace(output) == "sqlfluff is not installed")
6969
if sql_fluff_not_installed {
7070
log.Fatal(color.RedString("sqlfluff not installed. Please install sqlfluff using 'pip install sqlfluff'"))
7171
}
72-
sqlxFileMetaData.sqlBlocksMeta.formattedSqlBlockContent = output
72+
sqlxFileMetaData.sqlBlocksMeta.formattedSqlBlockContent = output
7373
return nil
7474
}
7575

7676
func finalFormmatedSqlxFileContents(sqlxFileMetaData *sqlxParserMeta) string {
77-
spaceBetweenBlocks := "\n\n"
78-
spaceBetweenSameOps := "\n"
79-
80-
formattedQuery := ""
81-
prePostOpBlock := ""
82-
83-
preOpsBlocks := sqlxFileMetaData.preOpsBlocksMeta
84-
postOpsBlocks := sqlxFileMetaData.postOpsBlocksMeta
85-
86-
preOpsBlockContent := ""
87-
if len(preOpsBlocks) > 0 {
88-
for _, preOpsBlock := range preOpsBlocks {
89-
preOpsBlockContent += preOpsBlock.preOpsBlockContent + spaceBetweenSameOps
90-
}
91-
}
92-
93-
postOpsBlockContent := ""
94-
if len(postOpsBlocks) > 0 {
95-
for _, postOpsBlock := range postOpsBlocks {
96-
postOpsBlockContent += postOpsBlock.postOpsBlockContent + spaceBetweenSameOps
97-
}
98-
}
99-
100-
if preOpsBlockContent == "" && postOpsBlockContent == "" {
101-
prePostOpBlock = ""
102-
} else {
103-
prePostOpBlock = spaceBetweenBlocks + preOpsBlockContent + spaceBetweenBlocks + postOpsBlockContent
104-
}
105-
106-
formattedQuery = sqlxFileMetaData.configBlockMeta.configBlockContent +
107-
prePostOpBlock +
108-
spaceBetweenBlocks +
109-
sqlxFileMetaData.sqlBlocksMeta.formattedSqlBlockContent
110-
return formattedQuery
77+
spaceBetweenBlocks := "\n\n"
78+
// NOTE: Dataform at the time of writing this does not really have multiple preOpsBlocks or postOpsBlocks in its compiled json although it does not throw a compilation error if you put one
79+
spaceBetweenSameOps := "\n"
80+
81+
formattedQuery := ""
82+
prePostOpBlock := ""
83+
84+
preOpsBlocks := sqlxFileMetaData.preOpsBlocksMeta
85+
postOpsBlocks := sqlxFileMetaData.postOpsBlocksMeta
86+
87+
preOpsBlockContent := ""
88+
if len(preOpsBlocks) > 0 {
89+
for _, preOpsBlock := range preOpsBlocks {
90+
preOpsBlockContent += preOpsBlock.preOpsBlockContent + spaceBetweenSameOps
91+
}
92+
}
93+
94+
postOpsBlockContent := ""
95+
if len(postOpsBlocks) > 0 {
96+
for _, postOpsBlock := range postOpsBlocks {
97+
postOpsBlockContent += postOpsBlock.postOpsBlockContent + spaceBetweenSameOps
98+
}
99+
}
100+
101+
if preOpsBlockContent == "" && postOpsBlockContent == "" {
102+
prePostOpBlock = ""
103+
} else {
104+
prePostOpBlock = spaceBetweenBlocks + preOpsBlockContent + spaceBetweenBlocks + postOpsBlockContent
105+
}
106+
107+
formattedQuery = sqlxFileMetaData.configBlockMeta.configBlockContent +
108+
prePostOpBlock +
109+
spaceBetweenBlocks +
110+
sqlxFileMetaData.sqlBlocksMeta.formattedSqlBlockContent
111+
return formattedQuery
111112
}
112113

113114
func writeContentsToFile(sqlxFileMetaData *sqlxParserMeta, formattingError error) {
114115

115116
yellow := color.New(color.FgYellow).SprintFunc()
116117
red := color.New(color.FgRed).SprintFunc()
117118

118-
filPathSeparator := string(os.PathSeparator)
119-
_definitions := "definitions" + filPathSeparator
119+
filPathSeparator := string(os.PathSeparator)
120+
_definitions := "definitions" + filPathSeparator
120121
baseFilepath := strings.Split(sqlxFileMetaData.filepath, _definitions)
121-
formattedFilePath := filepath.Join("formatted", "definitions", baseFilepath[1])
122+
formattedFilePath := filepath.Join("formatted", "definitions", baseFilepath[1])
122123

123124
dirToCreate := formattedFilePath[:strings.LastIndex(formattedFilePath, filPathSeparator)]
124125

125126
os.MkdirAll(dirToCreate, 0755) // TODO: make this configurable
126127

127-
formattedQuery := finalFormmatedSqlxFileContents(sqlxFileMetaData)
128+
formattedQuery := finalFormmatedSqlxFileContents(sqlxFileMetaData)
128129

129130
err := os.WriteFile(formattedFilePath, []byte(formattedQuery), 0664)
130131
if err != nil {
@@ -145,7 +146,7 @@ func writeContentsToFileInPlace(sqlxFileMetaData *sqlxParserMeta, formattingErro
145146
yellow := color.New(color.FgYellow).SprintFunc()
146147
red := color.New(color.FgRed).SprintFunc()
147148

148-
formattedQuery := finalFormmatedSqlxFileContents(sqlxFileMetaData)
149+
formattedQuery := finalFormmatedSqlxFileContents(sqlxFileMetaData)
149150

150151
err := os.WriteFile(sqlxFileMetaData.filepath, []byte(formattedQuery), 0664)
151152
if err != nil {
@@ -162,13 +163,13 @@ func writeContentsToFileInPlace(sqlxFileMetaData *sqlxParserMeta, formattingErro
162163
}
163164

164165
func formatSqlxFile(sqlxFilePath string, inplace bool, sqlfluffConfigPath string, pythonExecutable string, logger *slog.Logger) {
165-
sqlxFileMetaData, err := sqlxParser(sqlxFilePath)
166-
// fmt.Printf("%+v\n", sqlxFileMetaData)
166+
sqlxFileMetaData, err := sqlxParser(sqlxFilePath)
167+
// fmt.Printf("%+v\n", sqlxFileMetaData)
167168

168169
if err != nil {
169170
fmt.Println("Error finding config blocks:", err)
170171
} else {
171-
pythonScriptPath := filepath.Join(".formatdataform", "sqlfluff_formatter.py")
172+
pythonScriptPath := filepath.Join(".formatdataform", "sqlfluff_formatter.py")
172173
formattingError := formatSqlCode(&sqlxFileMetaData, pythonScriptPath, sqlfluffConfigPath, pythonExecutable, logger)
173174
if inplace {
174175
writeContentsToFileInPlace(&sqlxFileMetaData, formattingError)
@@ -197,7 +198,7 @@ func createFileFromText(text string, filepath string) error {
197198
return err
198199
} else {
199200
f.WriteString(text)
200-
fmt.Printf("file created at: `%s` \n", filepath)
201+
fmt.Printf("file created at: `%s` \n", filepath)
201202
f.Close()
202203
}
203204
return nil

0 commit comments

Comments
 (0)