Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,27 @@ tasks:
desc: Generate Go code from all schema examples.
dir: "{{.git_root}}"
cmds:
- rm -rf tests/localstack/generated
- mkdir -p tests/localstack/generated
- |
for schema in tests/fixtures/*.json; do
- rm -rf tests/localstack/generated
- mkdir -p tests/localstack/generated
- |
for schema in tests/fixtures/*.json; do
filename=$(basename "${schema}" .json)

if [[ "${filename}" == *__* ]]; then
mode="${filename##*__}"
echo "Generating with mode: ${mode} for ${schema}"

go run ./cmd/dyno generate \
-schema "${schema}" \
-output-dir "tests/localstack/generated" \
-mode "${mode}"
else
echo "Generating without mode for ${schema}"
go run ./cmd/dyno generate \
-schema "${schema}" \
-output-dir "tests/localstack/generated"
done
fi
done
silent: true
internal: true

Expand Down
12 changes: 11 additions & 1 deletion internal/app/commands/generate/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Mad-Pixels/go-dyno/internal/app/flags"
"github.com/Mad-Pixels/go-dyno/internal/generator"
"github.com/Mad-Pixels/go-dyno/internal/generator/mode"
"github.com/Mad-Pixels/go-dyno/internal/logger"
"github.com/Mad-Pixels/go-dyno/internal/utils/conv"
"github.com/Mad-Pixels/go-dyno/internal/utils/writer"
Expand All @@ -16,10 +17,18 @@ func action(ctx *cli.Context) error {
var (
schemaPath = ctx.String(flags.LocalSchema.GetName())
outputPath = ctx.String(flags.LocalOutputDir.GetName())
modeRaw = ctx.String(flags.LocalGenerateMode.GetName())
)

m, err := mode.ParseMode(modeRaw)
if err != nil {
return err
}

logger.Log.Debug().
Str("schema", schemaPath).
Str("output", outputPath).
Str("mode", m.String()).
Msg("Starting code generation")

g, err := generator.NewGenerator(schemaPath)
Expand All @@ -30,7 +39,8 @@ func action(ctx *cli.Context) error {
return err
}

builder := g.NewRenderBuilder()
builder := g.NewRenderBuilder().
WithMode(m)
if ctx.IsSet(flags.LocalPackageName.GetName()) {
var (
raw = ctx.String(flags.LocalPackageName.GetName())
Expand Down
3 changes: 3 additions & 0 deletions internal/app/commands/generate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type tmplUsage struct {
EnvPrefix string

FlagSchemaPath string
FlagMode string
}

// Command entrypoint.
Expand All @@ -30,6 +31,7 @@ func Command() *cli.Command {
EnvPrefix: godyno.EnvPrefix,

FlagSchemaPath: flags.LocalSchema.GetName(),
FlagMode: flags.LocalGenerateMode.GetName(),
},
)

Expand All @@ -44,6 +46,7 @@ func Command() *cli.Command {
flags.LocalOutputDir.Object,
flags.LocalFilename.Object,
flags.LocalPackageName.Object,
flags.LocalGenerateMode.Object,
},
}
}
18 changes: 18 additions & 0 deletions internal/app/flags/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

godyno "github.com/Mad-Pixels/go-dyno"
"github.com/Mad-Pixels/go-dyno/internal/generator/mode"

"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -68,4 +69,21 @@ var (
Required: false,
},
}

// LocalGenerateMode defines the --mode flag for controlling code generation mode.
// Determines what code to generate: ALL (complete) or MIN (minimal).
LocalGenerateMode = Flag{
Object: &cli.StringFlag{
Name: "mode",
Usage: fmt.Sprintf("Set generation mode (%s). (default: %s)", strings.Join(mode.GetAvailableModes(), ", "), mode.GetDefault()),
Aliases: []string{
"m",
},
EnvVars: []string{
fmt.Sprintf("%s_%s", godyno.EnvPrefix, strings.ToUpper("mode")),
},
Required: false,
Value: mode.GetDefault().String(),
},
}
)
19 changes: 19 additions & 0 deletions internal/generator/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generator

import (
"github.com/Mad-Pixels/go-dyno/internal/generator/mode"
"github.com/Mad-Pixels/go-dyno/internal/logger"
"github.com/Mad-Pixels/go-dyno/internal/utils/conv"
"github.com/Mad-Pixels/go-dyno/internal/utils/tmpl"
Expand All @@ -11,6 +12,7 @@ import (
// Allows overriding schema defaults (package name, filename) via CLI flags.
type RenderBuilder struct {
generator *Generator
mode *mode.Mode
packageName *string
filename *string
}
Expand All @@ -33,6 +35,14 @@ func (rb *RenderBuilder) WithFilename(name string) *RenderBuilder {
return rb
}

// WithMode overrides the generator mode type.
func (rb *RenderBuilder) WithMode(mode mode.Mode) *RenderBuilder {
if mode.IsValid() {
rb.mode = &mode
}
return rb
}

// Build renders the final Go code using configured overrides.
func (rb *RenderBuilder) Build() string {
var (
Expand Down Expand Up @@ -60,6 +70,14 @@ func (rb *RenderBuilder) GetFilename() string {
return rb.generator.schema.Filename()
}

// GetMode returns the current generation mode (or default if not set).
func (rb *RenderBuilder) GetMode() mode.Mode {
if rb.mode != nil {
return *rb.mode
}
return mode.GetDefault()
}

// buildTemplateMap creates template data with schema and overrides.
func (rb *RenderBuilder) buildTemplateMap() v2.TemplateMap {
schema := rb.generator.schema
Expand All @@ -69,6 +87,7 @@ func (rb *RenderBuilder) buildTemplateMap() v2.TemplateMap {
TableName: schema.TableName(),
HashKey: schema.HashKey(),
RangeKey: schema.RangeKey(),
Mode: rb.GetMode(),
Attributes: schema.Attributes(),
CommonAttributes: schema.CommonAttributes(),
AllAttributes: schema.AllAttributes(),
Expand Down
91 changes: 91 additions & 0 deletions internal/generator/mode/mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Package mode defines the generation modes for code generation.
//
// It provides a type-safe enum for controlling what code is generated,
// allowing users to choose between generating all code (ALL) or just
// the minimum required code (MIN).
package mode

import (
"strings"

"github.com/Mad-Pixels/go-dyno/internal/logger"
"github.com/Mad-Pixels/go-dyno/internal/utils/conv"
)

// Mode represents the code generation mode.
type Mode string

const (
// ALL generates complete code with all features (default).
ALL Mode = "ALL"

// MIN generates minimal code with only essential functionality.
MIN Mode = "MIN"
)

// Valid modes for validation.
var validModes = map[Mode]bool{
ALL: true,
MIN: true,
}

// String returns the string representation of the Mode.
func (m Mode) String() string {
return string(m)
}

// IsValid checks if the mode is a valid generation mode.
func (m Mode) IsValid() bool {
return validModes[m]
}

// ParseMode parses a string into a Mode type with case-insensitive matching.
// Returns the parsed mode and an error if the string is not a valid mode.
func ParseMode(s string) (Mode, error) {
mode := Mode(strings.ToUpper(strings.TrimSpace(s)))
if !mode.IsValid() {
return "", logger.NewFailure("invalid generation mode", nil).
With("mode", s).
With("available", GetAvailableModes())
}
return mode, nil
}

// MustParseMode parses a string into a Mode type and panics on error.
// Should only be used in tests or when the input is guaranteed to be valid.
func MustParseMode(s string) Mode {
mode, err := ParseMode(s)
if err != nil {
panic(err)
}
return mode
}

// GetDefault returns the default generation mode.
func GetDefault() Mode {
return ALL
}

// GetAvailableModes returns a slice of all valid modes sorted alphabetically.
func GetAvailableModes() []string {
stringModes := make(map[string]bool, len(validModes))
for mode := range validModes {
stringModes[string(mode)] = true
}
return conv.AvailableKeys(stringModes)
}

// IsALL checks if the given mode is ALL.
func IsALL(m Mode) bool {
return m == ALL
}

// IsMIN checks if the given mode is MIN.
func IsMIN(m Mode) bool {
return m == MIN
}

// IsMode checks if the given mode matches the target mode string.
func IsMode(m Mode, target string) bool {
return m.String() == target
}
4 changes: 4 additions & 0 deletions internal/utils/tmpl/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"text/template"

"github.com/Mad-Pixels/go-dyno/internal/generator/attribute"
"github.com/Mad-Pixels/go-dyno/internal/generator/mode"
"github.com/Mad-Pixels/go-dyno/internal/logger"
"github.com/Mad-Pixels/go-dyno/internal/utils/conv"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -152,6 +153,9 @@ func renderTemplate(b *bytes.Buffer, tmpl string, vars any, shouldFormat bool) {
"GetUsedNumericSetTypes": attribute.GetUsedNumericSetTypes,
"IsFloatType": conv.IsFloatType,
"Slice": conv.TrimLeftN,
"IsALL": mode.IsALL,
"IsMIN": mode.IsMIN,
"IsMode": mode.IsMode,
},
).
Parse(tmpl)
Expand Down
39 changes: 39 additions & 0 deletions templates/v2/core/mixin_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package core

// KeyConditionMixinSugarTemplate provides convenience With methods (only for ALL mode)
const KeyConditionMixinSugarTemplate = `
// CONVENIENCE METHODS - Only available in ALL mode

// WithEQ adds equality key condition.
// Required for partition key, optional for sort key.
// Example: .WithEQ("user_id", "123")
func (kcm *KeyConditionMixin) WithEQ(field string, value any) {
kcm.With(field, EQ, value)
}

// WithBetween adds range key condition for sort keys.
// Example: .WithBetween("created_at", start_time, end_time)
func (kcm *KeyConditionMixin) WithBetween(field string, start, end any) {
kcm.With(field, BETWEEN, start, end)
}

// WithGT adds greater than key condition for sort keys.
func (kcm *KeyConditionMixin) WithGT(field string, value any) {
kcm.With(field, GT, value)
}

// WithGTE adds greater than or equal key condition for sort keys.
func (kcm *KeyConditionMixin) WithGTE(field string, value any) {
kcm.With(field, GTE, value)
}

// WithLT adds less than key condition for sort keys.
func (kcm *KeyConditionMixin) WithLT(field string, value any) {
kcm.With(field, LT, value)
}

// WithLTE adds less than or equal key condition for sort keys.
func (kcm *KeyConditionMixin) WithLTE(field string, value any) {
kcm.With(field, LTE, value)
}
`
Loading
Loading