Skip to content

Instantly share code, notes, and snippets.

@zdunecki
Created February 22, 2021 21:29
Show Gist options
  • Save zdunecki/308e53f32fb0723c281b41561ebe53ad to your computer and use it in GitHub Desktop.
Save zdunecki/308e53f32fb0723c281b41561ebe53ad to your computer and use it in GitHub Desktop.
Plugin for gqlgen to make json tags as snakecase
package main
import (
"fmt"
"github.com/99designs/gqlgen/api"
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin/modelgen"
"os"
"regexp"
"strings"
)
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
var jsonTagRegexp = regexp.MustCompile(`json:".*?"`)
var jsonTagGroupRegexp = regexp.MustCompile(`json:"(.*?)"`)
func snakeCaseMutateHook(b *modelgen.ModelBuild) *modelgen.ModelBuild {
for _, model := range b.Models {
for _, field := range model.Fields {
jsonTagGrouped := jsonTagGroupRegexp.FindStringSubmatch(field.Tag)
snakeCase := ToSnakeCase(jsonTagGrouped[1])
field.Tag = jsonTagRegexp.ReplaceAllString(field.Tag, fmt.Sprintf(`json:"%s"`, snakeCase))
}
}
return b
}
// That plugin is used for custom model generation
// It allow to generate model with snake case json tag e.g `json:"example_prop"`
func main() {
cfg, err := config.LoadConfigFromDefaultLocations()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
os.Exit(2)
}
// Attaching the mutation function onto modelgen plugin
p := modelgen.Plugin{
MutateHook: snakeCaseMutateHook,
}
err = api.Generate(cfg,
api.NoPlugins(),
api.AddPlugin(&p),
)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
}
os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment