Skip to content

Commit

Permalink
Fix/from slice ptr (#19)
Browse files Browse the repository at this point in the history
* refactor: update field

* refactor: update pointer checker

* fix: add from-slice-ptr
  • Loading branch information
snamiki1212 authored Sep 29, 2024
1 parent e72e987 commit 718f780
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Flags:
| [FromPtr](https://github.com/samber/lo?tab=readme-ov-file#fromptr) | ー | ー | ー |
| [FromPtrOr](https://github.com/samber/lo?tab=readme-ov-file#fromptror) | ー | ー | ー |
| [ToSlicePtr](https://github.com/samber/lo?tab=readme-ov-file#tosliceptr) | `[]T` | ✅ | ー |
| [FromSlicePtr](https://github.com/samber/lo?tab=readme-ov-file#fromsliceptr) | `[]T` | 🔨 | ー |
| [FromSlicePtr](https://github.com/samber/lo?tab=readme-ov-file#fromsliceptr) | `[]T` | | ー |
| [FromSlicePtrOr](https://github.com/samber/lo?tab=readme-ov-file#fromsliceptror) | `[]T` | 🔨 | ー |
| [ToAnySlice](https://github.com/samber/lo?tab=readme-ov-file#toanyslice) | `[]T` | ー | ー |
| [FromAnySlice](https://github.com/samber/lo?tab=readme-ov-file#fromanyslice) | ー | ー | ー |
Expand Down
5 changes: 5 additions & 0 deletions example/users_ptr_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 13 additions & 22 deletions internal/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@ import (
)

type Arguments struct {
// Target Entity name
Entity string

// Entitty is pointer or not
IsPtrEntity bool
// Filename
Input string // for input
Output string // for output

// Target Slice name
Slice string

// Input file name
Input string

// Output file name
Output string

// Rename flag that is mapping field name to accessor name
RawRename []string
renameInfoList []renameInfo

// Raw entity
RawEntity string
// Entity
RawEntity string // with maybe pointer (ex. *User or User)
Entity string // without pointer (ex. User)

// Excluded lo methods
RawLoMethodsToExclude []string
Expand Down Expand Up @@ -70,11 +63,13 @@ func (a *Arguments) Load() error {
return nil
}

// EntityIsPtr
func (a Arguments) EntityIsPtr() bool {
return strings.HasPrefix(a.Entity, "*")
}

func (a Arguments) DisplayEntity() string {
if a.IsPtrEntity {
return "*" + a.Entity
}
return a.Entity
return a.RawEntity
}

type renameInfo struct {
Expand Down Expand Up @@ -145,11 +140,7 @@ func (a *Arguments) loadRename(as []string) error {

// load Entity flag
func (a *Arguments) loadEntity(e string) error {
a.IsPtrEntity = strings.HasPrefix(e, "*")
if a.IsPtrEntity {
e = strings.TrimPrefix(e, "*")
}
a.Entity = e
a.Entity = strings.TrimPrefix(e, "*")
return nil
}

Expand Down
9 changes: 8 additions & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@ func (g Generator) genStd(args internal.Arguments, sliceName string) (string, er
// Get method name
method, _ := args.Rename(elem.StdName())

// Generate data
data := &loStdTemplateMapper{Slice: sliceName, Entity: args.DisplayEntity(), Method: method, EntityName: args.Entity}

// Skip generate
if elem.Skip(*data) {
continue
}

// Generate code block from template
data := &loStdTemplateMapper{Slice: sliceName, Entity: args.DisplayEntity(), Method: method}
if err = tp.Execute(&doc, data); err != nil {
return "", fmt.Errorf("template execute error: %w", err)
}
Expand Down
11 changes: 8 additions & 3 deletions internal/generator/lo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ type Lo interface {

// Template for lo with extend. Return false if not implemented.
ExtendTemplate() (string, bool)

// Skip generate lo method if true.
Skip(params loStdTemplateMapper) bool
}

// Replace variable from key to value in template.
type loStdTemplateMapper struct {
Slice string // Slice name for target struct (ex. Users).
Entity string // Entity name for target struct (ex. User / *User).
Method string // Method name of struct (ex. Filter).
Slice string // Slice name for target struct (ex. Users).
Entity string // Entity name with pointer for target struct (ex. User / *User).
EntityName string // Entity name without pointer for target struct (ex. User ).
Method string // Method name of struct (ex. Filter).
}

// Replace variable from key to value in template.
Expand All @@ -42,5 +46,6 @@ func NewAllLoList() []Lo {
NewLoEveryBy(),
NewLoSomeBy(),
NewLoToSlicePtr(),
NewLoFromSlicePtr(),
}
}
2 changes: 2 additions & 0 deletions internal/generator/lo_contains_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoContainsBy struct{}

func NewLoContainsBy() LoContainsBy { return LoContainsBy{} }

func (l LoContainsBy) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoContainsBy) StdName() string { return "ContainsBy" }

func (l LoContainsBy) ExtendName() string { return "ContainsBy" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_evenry_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoEveryBy struct{}

func NewLoEveryBy() LoEveryBy { return LoEveryBy{} }

func (l LoEveryBy) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoEveryBy) StdName() string { return "EveryBy" }

func (l LoEveryBy) ExtendName() string { return "EveryBy" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoFilter struct{}

func NewLoFilter() LoFilter { return LoFilter{} }

func (l LoFilter) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoFilter) StdName() string { return "Filter" }

func (l LoFilter) ExtendName() string { return "FilterBy" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_filter_reject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoFilterReject struct{}

func NewLoFilterReject() LoFilterReject { return LoFilterReject{} }

func (l LoFilterReject) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoFilterReject) StdName() string { return "FilterReject" }

func (l LoFilterReject) ExtendName() string { return "FilterRejectBy" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoFind struct{}

func NewLoFind() LoFind { return LoFind{} }

func (l LoFind) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoFind) StdName() string { return "Find" }

func (l LoFind) ExtendName() string { return "FindBy" }
Expand Down
29 changes: 29 additions & 0 deletions internal/generator/lo_from_slice_ptr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package generator

import "strings"

type LoFromSlicePtr struct{}

func NewLoFromSlicePtr() LoFromSlicePtr { return LoFromSlicePtr{} }

func (l LoFromSlicePtr) Skip(params loStdTemplateMapper) bool {
isPtr := strings.HasPrefix(params.Entity, "*")
return !isPtr // run for only value slices
}

func (l LoFromSlicePtr) StdName() string { return "FromSlicePtr" }

func (l LoFromSlicePtr) ExtendName() string { return "" }

func (l LoFromSlicePtr) StdTemplate() (string, bool) {
return `
// {{ .Method }}
func (xs {{ .Slice }}) {{ .Method }}() []{{ .EntityName }} {
return lo.FromSlicePtr(xs)
}
`, true
}

func (l LoFromSlicePtr) ExtendTemplate() (string, bool) {
return ``, false
}
2 changes: 2 additions & 0 deletions internal/generator/lo_group_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoGroupBy struct{}

func NewLoGroupBy() LoGroupBy { return LoGroupBy{} }

func (l LoGroupBy) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoGroupBy) StdName() string { return "GroupBy" }

func (l LoGroupBy) ExtendName() string { return "GroupBy" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_key_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoKeyBy struct{}

func NewLoKeyBy() LoKeyBy { return LoKeyBy{} }

func (l LoKeyBy) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoKeyBy) StdName() string { return "KeyBy" }

func (l LoKeyBy) ExtendName() string { return "KeyBy" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_some_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoSomeBy struct{}

func NewLoSomeBy() LoSomeBy { return LoSomeBy{} }

func (l LoSomeBy) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoSomeBy) StdName() string { return "SomeBy" }

func (l LoSomeBy) ExtendName() string { return "SomeBy" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_to_slice_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoToSlicePtr struct{}

func NewLoToSlicePtr() LoToSlicePtr { return LoToSlicePtr{} }

func (l LoToSlicePtr) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoToSlicePtr) StdName() string { return "ToSlicePtr" }

func (l LoToSlicePtr) ExtendName() string { return "" }
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/lo_uniq_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type LoUniqBy struct{}

func NewLoUniqBy() LoUniqBy { return LoUniqBy{} }

func (l LoUniqBy) Skip(_ loStdTemplateMapper) bool { return false }

func (l LoUniqBy) StdName() string { return "UniqBy" }

func (l LoUniqBy) ExtendName() string { return "UniqBy" }
Expand Down

0 comments on commit 718f780

Please sign in to comment.