-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.go
87 lines (71 loc) · 2.31 KB
/
features.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
package handlers
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"github.com/nachatz/my-ai-maker/app/internal/gen"
"github.com/nachatz/my-ai-maker/app/internal/models"
"github.com/nachatz/my-ai-maker/app/internal/utils"
)
func FeatureHandler(w http.ResponseWriter, r *http.Request) {
/* FeatureHandler - Handles the HTTP request for processing JSON features and data types.
@Param w - The http.ResponseWriter to write the response to.
@Param r - The *http.Request representing the incoming request.
*/
var response models.Response
// Parse the JSON request body
var featureRequest models.FeatureRequest
if err := json.NewDecoder(r.Body).Decode(&featureRequest); err != nil {
response.Message = "Failed to parse JSON request"
response.StatusCode = http.StatusBadRequest
utils.WriteResponse(w, response)
return
}
// Validate required fields
if err := validateFeatureRequest(featureRequest); err != nil {
response.Message = "Validation failed: " + err.Error()
response.StatusCode = http.StatusBadRequest
utils.WriteResponse(w, response)
return
}
// Process the JSON features and data types
code, err := processFeatures(featureRequest)
if err != nil {
response.Message = "Failed to process features: " + err.Error()
response.StatusCode = http.StatusInternalServerError
utils.WriteResponse(w, response)
return
}
response.Message = code
response.StatusCode = http.StatusOK
utils.WriteResponse(w, response)
}
func processFeatures(features models.FeatureRequest) (string, error) {
/* processFeatures - Handles the mapping of features from posted json
@Param features - Map of feature metadata
*/
for feature, dataType := range features.Features {
log.Printf("Feature: %s, DataType: %s", feature, dataType)
}
code, err := gen.GenerateCode(features)
if err != nil {
return "", fmt.Errorf("failed to generate code: %v", err)
}
fmt.Printf("Output code:\n%s\n", code)
return code, nil
}
func validateFeatureRequest(request models.FeatureRequest) error {
// Use a validator library or manually validate each field
if len(request.Features) == 0 {
return errors.New("features field is required")
}
if request.Language == "" {
return errors.New("language field is required")
}
if request.Library == "" {
return errors.New("library field is required")
}
return nil
}