Skip to content

Commit

Permalink
write Models.pm
Browse files Browse the repository at this point in the history
  • Loading branch information
kfly8 committed Aug 4, 2024
1 parent 52b0d45 commit 70a22b5
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 7 deletions.
80 changes: 73 additions & 7 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,94 @@ package perl

import (
//"bufio"
//"bytes"
"bufio"
"bytes"
"context"

//"errors"
//"fmt"
//"go/format"
//"strings"
//"text/template"
"strings"
"text/template"

"github.com/kfly8/sqlc-gen-perl/internal/opts"
"github.com/sqlc-dev/plugin-sdk-go/sdk"

//"github.com/sqlc-dev/plugin-sdk-go/sdk"
//"github.com/sqlc-dev/plugin-sdk-go/metadata"
"github.com/sqlc-dev/plugin-sdk-go/plugin"
)


type tmplCtx struct {
Q string
SqlcVersion string
SourceName string
}

func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
return generate(req)
options, err := opts.Parse(req)
if err != nil {
return nil, err
}

if err := opts.ValidateOpts(options); err != nil {
return nil, err
}

return generate(req, options)
}


func generate(req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
func generate(req *plugin.GenerateRequest, options *opts.Options) (*plugin.GenerateResponse, error) {

tctx := tmplCtx{
Q: "`",
SqlcVersion: req.SqlcVersion,
}

funcMap := template.FuncMap{
"lowerTitle": sdk.LowerTitle,
"comment": sdk.DoubleSlashComment,
"escape": sdk.EscapeBacktick,
"hasPrefix": strings.HasPrefix,
}

tmpl := template.Must(
template.New("table").
Funcs(funcMap).
ParseFS(
templates,
"templates/*.tmpl",
),
)

output := map[string]string{}
output["Query.pm"] = "package Query";

execute := func(name, templateName string) error {
var b bytes.Buffer
w := bufio.NewWriter(&b)
tctx.SourceName = name
err := tmpl.ExecuteTemplate(w, templateName, &tctx)
w.Flush()
if err != nil {
return err
}

if !strings.HasSuffix(name, ".pm") {
name += ".pm"
}

output[name] = b.String()
return nil
}

modelsFileName := "Models.pm"
if options.OutputModelsFileName != "" {
modelsFileName = options.OutputModelsFileName
}
if err := execute(modelsFileName, "modelsFile"); err != nil {
return nil, err
}

resp := plugin.GenerateResponse{}

Expand Down
42 changes: 42 additions & 0 deletions internal/opts/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package opts

import (
"encoding/json"
"fmt"
//"maps"
///"path/filepath"

"github.com/sqlc-dev/plugin-sdk-go/plugin"
)

type Options struct {
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
}

func Parse(req *plugin.GenerateRequest) (*Options, error) {
options, err := parseOpts(req)
if err != nil {
return nil, err
}

return options, nil
}

func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
var options Options
if len(req.PluginOptions) == 0 {
return &options, nil
}
if err := json.Unmarshal(req.PluginOptions, &options); err != nil {
return nil, fmt.Errorf("unmarshalling plugin options: %w", err)
}

return &options, nil
}

func ValidateOpts(opts *Options) error {
// TODO: validate options

return nil
}

6 changes: 6 additions & 0 deletions internal/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package perl

import "embed"

//go:embed templates/*
var templates embed.FS
17 changes: 17 additions & 0 deletions internal/templates/template.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{define "modelsFile"}}
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc: {{.SqlcVersion}}

package Models;
use strict;
use warnings;

{{template "modelsCode" .}}

1;
{{end}}

{{define "modelsCode"}}
Hello
{{end}}
7 changes: 7 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package perl

import "embed"

//go:embed templates/*

Check failure on line 5 in template.go

View workflow job for this annotation

GitHub Actions / test

pattern templates/*: no matching files found
//go:embed templates/*/*
var templates embed.FS

0 comments on commit 70a22b5

Please sign in to comment.