-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
A first step towards protecting services and packing annotations from having mismatched names e.g. `service Foobar` and `option c_sharp_namespace = "Google.FooBar.V1"` where `Foobar` and `FooBar` do not match in casing. @jskeet
- Loading branch information
Showing
7 changed files
with
150 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,42 +27,60 @@ import ( | |
|
||
var csharpNamespace = &lint.FileRule{ | ||
Name: lint.NewRuleName(191, "csharp-namespace"), | ||
OnlyIf: func(f *desc.FileDescriptor) bool { | ||
fops := f.GetFileOptions() | ||
return fops != nil && fops.GetCsharpNamespace() != "" | ||
}, | ||
LintFile: func(f *desc.FileDescriptor) []lint.Problem { | ||
if ns := f.GetFileOptions().GetCsharpNamespace(); ns != "" { | ||
// Check for invalid characters. | ||
if !csharpValidChars.MatchString(ns) { | ||
return []lint.Problem{{ | ||
Message: "Invalid characters: C# namespaces only allow [A-Za-z0-9.].", | ||
Descriptor: f, | ||
Location: locations.FileCsharpNamespace(f), | ||
}} | ||
} | ||
ns := f.GetFileOptions().GetCsharpNamespace() | ||
delim := "." | ||
|
||
// Check that upper camel case is used. | ||
upperCamel := []string{} | ||
for _, segment := range strings.Split(ns, ".") { | ||
wantSegment := csharpVersionRegexp.ReplaceAllStringFunc( | ||
strcase.UpperCamelCase(segment), | ||
func(s string) string { | ||
point := csharpVersionRegexp.FindStringSubmatch(s)[1] | ||
if point != "" { | ||
s = strings.ReplaceAll(s, point, strings.ToUpper(point)) | ||
} | ||
stability := csharpVersionRegexp.FindStringSubmatch(s)[2] | ||
return strings.ReplaceAll(s, stability, strings.Title(stability)) | ||
}, | ||
) | ||
upperCamel = append(upperCamel, wantSegment) | ||
} | ||
if want := strings.Join(upperCamel, "."); ns != want { | ||
// Check for invalid characters. | ||
if !csharpValidChars.MatchString(ns) { | ||
return []lint.Problem{{ | ||
Message: "Invalid characters: C# namespaces only allow [A-Za-z0-9.].", | ||
This comment has been minimized.
Sorry, something went wrong. |
||
Descriptor: f, | ||
Location: locations.FileCsharpNamespace(f), | ||
}} | ||
} | ||
|
||
// Check that upper camel case is used. | ||
upperCamel := []string{} | ||
for _, segment := range strings.Split(ns, delim) { | ||
wantSegment := csharpVersionRegexp.ReplaceAllStringFunc( | ||
strcase.UpperCamelCase(segment), | ||
func(s string) string { | ||
point := csharpVersionRegexp.FindStringSubmatch(s)[1] | ||
if point != "" { | ||
s = strings.ReplaceAll(s, point, strings.ToUpper(point)) | ||
} | ||
stability := csharpVersionRegexp.FindStringSubmatch(s)[2] | ||
return strings.ReplaceAll(s, stability, strings.Title(stability)) | ||
}, | ||
) | ||
upperCamel = append(upperCamel, wantSegment) | ||
} | ||
if want := strings.Join(upperCamel, delim); ns != want { | ||
return []lint.Problem{{ | ||
Message: "C# namespaces use UpperCamelCase.", | ||
Suggestion: fmt.Sprintf("option csharp_namespace = %q;", want), | ||
Descriptor: f, | ||
Location: locations.FileCsharpNamespace(f), | ||
}} | ||
} | ||
|
||
for _, s := range f.GetServices() { | ||
n := s.GetName() | ||
if !packagingServiceNameEquals(n, ns, delim) { | ||
msg := fmt.Sprintf("Case of C# namespace and service name %q must match.", n) | ||
This comment has been minimized.
Sorry, something went wrong.
jskeet
|
||
return []lint.Problem{{ | ||
Message: "C# namespaces use UpperCamelCase.", | ||
Suggestion: fmt.Sprintf("option csharp_namespace = %q;", want), | ||
Message: msg, | ||
Descriptor: f, | ||
Location: locations.FileCsharpNamespace(f), | ||
}} | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
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
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
Well, other characters are valid in C# namespaces, but we don't want them there :) (In particular, we don't want underscores.)