-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add test * Support regexp path * Add README about regexp support * Copy test directry to run paralley * Fix regular expression * Rename to d * Fix bug * Use pointer receiver for consistency * Use errors.New
- Loading branch information
Showing
8 changed files
with
1,327 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package importas | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type Config struct { | ||
RequiredAlias map[string]string | ||
Rules []*Rule | ||
} | ||
|
||
func (c *Config) CompileRegexp() error { | ||
rules := make([]*Rule, 0, len(c.RequiredAlias)) | ||
for path, alias := range c.RequiredAlias { | ||
reg, err := regexp.Compile(fmt.Sprintf("^%s$", path)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rules = append(rules, &Rule{ | ||
Regexp: reg, | ||
Alias: alias, | ||
}) | ||
} | ||
|
||
c.Rules = rules | ||
return nil | ||
} | ||
|
||
func (c *Config) findRule(path string) *Rule { | ||
for _, rule := range c.Rules { | ||
if rule.Regexp.MatchString(path) { | ||
return rule | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Config) AliasFor(path string) (string, bool) { | ||
rule := c.findRule(path) | ||
if rule == nil { | ||
return "", false | ||
} | ||
|
||
alias, err := rule.aliasFor(path) | ||
if err != nil { | ||
return "", false | ||
} | ||
|
||
return alias, true | ||
} | ||
|
||
type Rule struct { | ||
Alias string | ||
Regexp *regexp.Regexp | ||
} | ||
|
||
func (r *Rule) aliasFor(path string) (string, error) { | ||
str := r.Regexp.FindString(path) | ||
if len(str) > 0 { | ||
return r.Regexp.ReplaceAllString(str, r.Alias), nil | ||
} | ||
|
||
return "", errors.New("mismatch rule") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package c | ||
|
||
import ( | ||
v1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" // want `import "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" imported as "v1alpha1" but must be "autoscalingv1alpha1" according to config` | ||
v1 "knative.dev/serving/pkg/apis/serving/v1" // want `import "knative.dev/serving/pkg/apis/serving/v1" imported as "v1" but must be "servingv1" according to config` | ||
) | ||
|
||
func foo() { | ||
v1alpha1.Resource("") | ||
v1.Resource("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package c | ||
|
||
import ( | ||
autoscalingv1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" // want `import "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" imported as "v1alpha1" but must be "autoscalingv1alpha1" according to config` | ||
servingv1 "knative.dev/serving/pkg/apis/serving/v1" // want `import "knative.dev/serving/pkg/apis/serving/v1" imported as "v1" but must be "servingv1" according to config` | ||
) | ||
|
||
func foo() { | ||
autoscalingv1alpha1.Resource("") | ||
servingv1.Resource("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/julz/importas/testdata/src/c | ||
|
||
go 1.15 | ||
|
||
require knative.dev/serving v0.21.0 |
Oops, something went wrong.