Skip to content

Commit e90b5ed

Browse files
author
switchupcb
committed
fix custom option parser
1 parent 7b6d071 commit e90b5ed

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Each example has a **README**.
3434
| [tmpl](examples/tmpl/) | Uses `.tmpl` templates. |
3535
| [program](examples/program/) | Uses Copygen programmatically. |
3636

37-
This [example](examples/main/) uses three type-structs to generate the `ModelsToDomain()` function using a Command Line Interface.
37+
_*[`multi`](examples/_tests/multi/setup/setup.go) tests every type._
3838

39-
_[`multi`](examples/_tests/multi/setup/setup.go) tests every type._
39+
This [example](examples/main/) uses three type-structs to generate the `ModelsToDomain()` function using a Command Line Interface.
4040

4141
### Types
4242

@@ -131,7 +131,7 @@ You can specify options for your Copygen functions using comments: Do **NOT** pu
131131
| `depth field level` | Use a specific field depth. | Copygen uses full-field [depth](#depth) by default. <br /> Override this using `depth` with _regex_ and a [depth-level](#depth) integer. | `depth .* 2` <br /> `depth models.Account.* 1` |
132132
| `deepcopy field` | Deepcopy from-fields. | Copygen shallow copies fields by default. <br /> Override this using `deepcopy` with _regex_. <br /> For more info, view [Shallow Copy vs. Deep Copy](#shallow-copy-vs-deep-copy). | `deepcopy package.Type.Field` <br /> `deepcopy .*` _(all fields)_ |
133133
| `automatch field` | Use the automatcher selectively or with `map` and `tag`. | Using `map` or `tag` disables the default automatcher. <br /> Enable it using `automatch` with _regex_. <br /> | `automatch package.Type.Field` <br /> `automatch models.User.*` |
134-
| `custom option` | Specify custom options. | Use custom options with [templates](#templates). <br /> `custom` options are **function** options. <br /> Returns `map[string][]string` _(trim-spaced)_. | `ignore true` <br /> `swap false` |
134+
| `custom option` | Specify custom function options. | Use custom options with [templates](#templates). <br /> Returns `map[string][]string` _(trim-spaced)_. | `ignore true` <br /> `swap false` |
135135

136136
_[View a reference on Regex.](https://cheatography.com/davechild/cheat-sheets/regular-expressions/)_
137137

@@ -162,7 +162,7 @@ go install github.com/switchupcb/copygen@main
162162
163163
Install a specific version by specifying a tag.
164164
```
165-
go install github.com/switchupcb/copygen@v0.3.6
165+
go install github.com/switchupcb/copygen@v0.3.7
166166
```
167167
168168
Run the executable with given options.

cli/parser/function.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (p *Parser) parseFunctions(astcopygen *ast.InterfaceType) ([]models.Functio
5656
// map the function custom options.
5757
var customoptionmap map[string][]string
5858
for _, option := range fieldoptions {
59-
err := options.MapCustomOption(customoptionmap, option)
59+
customoptionmap, err = options.MapCustomOption(customoptionmap, option)
6060
if err != nil {
6161
fmt.Printf("WARNING: %v\n", err)
6262
}

cli/parser/keep.go

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func (p *Parser) assignFieldOption(comments []*ast.Comment) error {
6060

6161
for _, comment := range comments {
6262
text := comment.Text
63+
64+
// do NOT parse comments that have already been parsed.
6365
if p.Options.CommentOptionMap[text] != nil {
6466
continue
6567
}

cli/parser/options/custom.go

+9-22
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,15 @@ package options
22

33
import (
44
"fmt"
5-
"strings"
65
)
76

87
const (
98
CategoryCustom = "custom"
109
FormatCustom = "<option><whitespaces><value>"
1110
)
1211

13-
// ParseCustom parses a custom option.
14-
func ParseCustom(option string) (*Option, error) {
15-
splitoption := strings.Fields(option)
16-
17-
if len(splitoption) == 0 {
18-
return nil, fmt.Errorf("there is an unspecified %s option at an unknown line", CategoryCustom)
19-
} else if len(splitoption) == 1 {
20-
return nil, fmt.Errorf("there is a misconfigured %s option: %q.\nIs it in format %s?", CategoryCustom, option, FormatCustom)
21-
}
22-
23-
return &Option{
24-
Category: splitoption[0],
25-
Value: splitoption[:1],
26-
}, nil
27-
}
28-
2912
// MapCustomOption maps a custom option in an optionmap[category][]values.
30-
func MapCustomOption(optionmap map[string][]string, option *Option) error {
13+
func MapCustomOption(optionmap map[string][]string, option *Option) (map[string][]string, error) {
3114
if optionmap == nil {
3215
optionmap = make(map[string][]string)
3316
}
@@ -38,18 +21,22 @@ func MapCustomOption(optionmap map[string][]string, option *Option) error {
3821
optionmap[k] = append(optionmap[k], v)
3922
}
4023
} else {
41-
return fmt.Errorf("failed to map custom option: %v", option.Value)
24+
return optionmap, fmt.Errorf("failed to map custom option: %v", option.Value)
4225
}
4326
}
4427

45-
return nil
28+
return optionmap, nil
4629
}
4730

4831
// MapCustomOptions maps options with custom categories in a list of options to a customoptionmap[category][]value.
4932
func MapCustomOptions(options []*Option) (map[string][]string, error) {
50-
var optionmap map[string][]string
33+
var (
34+
optionmap map[string][]string
35+
err error
36+
)
37+
5138
for _, option := range options {
52-
err := MapCustomOption(optionmap, option)
39+
optionmap, err = MapCustomOption(optionmap, option)
5340
if err != nil {
5441
return nil, err
5542
}

cli/parser/options/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ func SetFieldOptions(field *models.Field, fieldoptions []*Option) {
7878
case CategoryDeepcopy:
7979
SetDeepcopy(field, *option)
8080

81+
case CategoryCustom:
82+
SetConvert(field, *option)
8183
}
8284
}
8385
}

0 commit comments

Comments
 (0)