-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (78 loc) · 2.31 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
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"openapi-to-wit/openapi"
"openapi-to-wit/utils"
"openapi-to-wit/wit"
"gopkg.in/yaml.v2"
)
const OPENAPI_SPEC_PATH = "api/schemas/paths/openapi.yaml"
const WIT_PATH = "out/world.wit"
func main() {
schemaPath := OPENAPI_SPEC_PATH
openAPISpec, err := openapi.LoadOpenAPISchema(schemaPath)
if err != nil {
log.Fatalf("Error loading OpenAPI spec: %v", err)
}
// Generate the WIT representation for the OpenAPI spec
witString := wit.GenerateWITFromOpenAPI(openAPISpec)
// Write the WIT output to file
witFilePath := WIT_PATH
err = utils.WriteWITToFile(witFilePath, witString)
if err != nil {
log.Fatalf("Error writing WIT to file: %v", err)
}
fmt.Println("WIT successfully generated and written to", witFilePath)
}
// Simulating OpenAPI schema and generating Go struct
type OpenAPISchema struct {
Components struct {
Schemas map[string]Schema `yaml:"schemas"`
} `yaml:"components"`
}
type Schema struct {
Properties map[string]Property `yaml:"properties"` // Map of properties and their types (simple example)
Required []string `yaml:"required"` // Required fields
}
type Property struct {
Type string `yaml:"type"`
Format string `yaml:"format,omitempty"`
}
// LoadOpenAPISchema loads the OpenAPI schema from a file path
func LoadOpenAPISchema(filePath string) (*OpenAPISchema, error) {
fileData, err := ioutil.ReadFile(filepath.Clean(filePath))
if err != nil {
return nil, fmt.Errorf("failed to read OpenAPI schema file: %w", err)
}
var schema OpenAPISchema
err = yaml.Unmarshal(fileData, &schema)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal OpenAPI schema: %w", err)
}
return &schema, nil
}
// GenerateGoStruct generates Go struct code from the OpenAPI schema
func GenerateGoStruct(schema Schema, structName string) ComplexType {
var fields []Parameter
for name, property := range schema.Properties {
var goType WITType
switch property.Type {
case "string":
goType = WITTypeString
case "integer":
goType = WITTypeI32
// Add more OpenAPI to Go type mappings as needed
default:
goType = WITTypeString // Default to string for this example
}
fields = append(fields, Parameter{Name: name, Type: goType})
}
return ComplexType{
Name: structName,
IsStruct: true,
Fields: fields,
}
}