-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (76 loc) · 2.21 KB
/
main.go
File metadata and controls
87 lines (76 loc) · 2.21 KB
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 main demonstrates how to use custom plugins with apibconv.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/amer8/apibconv/pkg/converter"
"github.com/amer8/apibconv/pkg/format"
"github.com/amer8/apibconv/pkg/format/openapi"
"github.com/amer8/apibconv/pkg/model"
)
// MyCustomTransformer implements a custom transformation logic
type MyCustomTransformer struct{}
func (t *MyCustomTransformer) Transform(api *model.API) error {
fmt.Println(" [Plugin] Running custom transformation...")
api.Info.Title = fmt.Sprintf("[Plugin] %s", api.Info.Title)
api.Info.Description += "\n\nProcessed by MyCustomTransformer plugin."
return nil
}
func main() {
// Define the transformation function wrapper
transformFn := func(api *model.API) error {
transformer := &MyCustomTransformer{}
return transformer.Transform(api)
}
// Create a converter with the custom transform
conv, err := converter.New(
converter.WithTransform(transformFn),
)
if err != nil {
log.Fatalf("failed to create converter: %v", err)
}
// Register parsers and writers
conv.RegisterParser(openapi.NewParser())
conv.RegisterWriter(openapi.NewWriter(openapi.WithIndent(2)))
// Create a dummy input file for demonstration
inputFile, err := os.CreateTemp("", "input-*.yaml")
if err != nil {
log.Fatal(err)
}
defer func() {
if cerr := os.Remove(inputFile.Name()); cerr != nil {
log.Printf("Error removing temp file: %v", cerr)
}
}()
if _, err := inputFile.WriteString(`openapi: 3.0.0
info:
title: Original API
version: 1.0.0
paths: {}`); err != nil {
log.Printf("failed to write to input file: %v", err)
return
}
if cerr := inputFile.Close(); cerr != nil {
log.Printf("Error closing input file: %v", cerr)
}
// Re-open for reading
input, err := os.Open(inputFile.Name())
if err != nil {
log.Printf("failed to re-open input file: %v", err)
return
}
defer func() {
if cerr := input.Close(); cerr != nil {
log.Printf("Error closing input reader: %v", cerr)
}
}()
// Convert
fmt.Println("Running conversion with plugin...")
err = conv.Convert(context.Background(), input, os.Stdout, format.FormatOpenAPI, format.FormatOpenAPI)
if err != nil {
log.Printf("conversion failed: %v", err)
return
}
}