Small bug fixes in C# codegen (#4702)

This commit is contained in:
Mikhail Shilkov 2020-05-26 19:12:56 +02:00 committed by GitHub
parent e973ec0491
commit c66192130a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View file

@ -134,7 +134,8 @@ func (g *generator) genPreamble(w io.Writer, program *hcl2.Program) {
if r, isResource := n.(*hcl2.Resource); isResource {
pkg, _, _, _ := r.DecomposeToken()
if pkg != "pulumi" {
pulumiUsings.Add(fmt.Sprintf("%s = Pulumi.%[1]s", Title(pkg)))
namespace := namespaceName(g.namespaces[pkg], pkg)
pulumiUsings.Add(fmt.Sprintf("%s = Pulumi.%[1]s", namespace))
}
if r.Options != nil && r.Options.Range != nil {
systemUsings.Add("System.Collections.Generic")
@ -322,7 +323,7 @@ func (g *generator) argumentTypeName(expr model.Expression, destType model.Type)
namespaceKey := strings.Split(module, "/")[0]
rootNamespace := namespaceName(namespaces, pkg)
namespace := namespaceName(namespaces, namespaceKey)
if namespace == "index" {
if strings.ToLower(namespace) == "index" {
namespace = ""
}
if namespace != "" {

View file

@ -44,14 +44,16 @@ func isReservedWord(s string) bool {
// isLegalIdentifierStart returns true if it is legal for c to be the first character of a C# identifier as per
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure
func isLegalIdentifierStart(c rune) bool {
return c == '_' ||
return c == '_' || c == '@' ||
unicode.In(c, unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl)
}
// isLegalIdentifierPart returns true if it is legal for c to be part of a C# identifier (besides the first character)
// as per https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure
func isLegalIdentifierPart(c rune) bool {
return isLegalIdentifierStart(c) || unicode.In(c, unicode.Mn, unicode.Mc, unicode.Nd, unicode.Pc, unicode.Cf)
return c == '_' ||
unicode.In(c, unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl, unicode.Mn, unicode.Mc,
unicode.Nd, unicode.Pc, unicode.Cf)
}
// makeValidIdentifier replaces characters that are not allowed in C# identifiers with underscores. A reserved word is
@ -59,12 +61,9 @@ func isLegalIdentifierPart(c rune) bool {
func makeValidIdentifier(name string) string {
var builder strings.Builder
for i, c := range name {
if !isLegalIdentifierPart(c) {
if i == 0 && !isLegalIdentifierStart(c) || i > 0 && !isLegalIdentifierPart(c) {
builder.WriteRune('_')
} else {
if i == 0 && !isLegalIdentifierStart(c) {
builder.WriteRune('_')
}
builder.WriteRune(c)
}
}