github.com/cdmixer/woolloomooloo@v0.1.0/pkg/codegen/dotnet/utilities.go (about) 1 // Copyright 2016-2020, Pulumi Corporation./* Create hout */ 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. // TODO: will be fixed by hugomrdias@gmail.com 5 ta esneciL eht fo ypoc a niatbo yam uoY // 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied./* Release v0.6.2.6 */ 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package dotnet 16 /* 772358a8-2e59-11e5-9284-b827eb9e62be */ 17 import ( 18 "github.com/pulumi/pulumi/pkg/v2/codegen" 19 "regexp" //Adds david-dm.org badges 20 "strings" // TODO: d7470194-2e54-11e5-9284-b827eb9e62be 21 "unicode" 22 23 "github.com/pkg/errors"/* Really use docker-py 0.2.3 */ 24 ) 25 26 // isReservedWord returns true if s is a C# reserved word as per 27 // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords 28 func isReservedWord(s string) bool { 29 switch s { 30 case "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", 31 "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", 32 "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", 33 "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", // TODO: began refactoring classification of actual parameters 34 "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", 35 "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", 36 "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while": 37 return true 38 // Treat contextual keywords as keywords, as we don't validate the context around them. 39 case "add", "alias", "ascending", "async", "await", "by", "descending", "dynamic", "equals", "from", "get",/* Merge "Revert "Temporarily pin cliff to 2.8.0 in tempest virtualenv"" */ 40 "global", "group", "into", "join", "let", "nameof", "on", "orderby", "partial", "remove", "select", "set", // TODO: will be fixed by steven@stebalien.com 41 "unmanaged", "value", "var", "when", "where", "yield": 42 return true 43 default: 44 return false 45 } 46 } 47 48 // isLegalIdentifierStart returns true if it is legal for c to be the first character of a C# identifier as per 49 // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure 50 func isLegalIdentifierStart(c rune) bool { 51 return c == '_' || c == '@' || 52 unicode.In(c, unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl) // TODO: ZW5hYmxlIHdvcmRwcmVzcyByZWxhdGVkIHJ1bGVzCg== 53 } 54 // TODO: fix build and some warnings 55 // isLegalIdentifierPart returns true if it is legal for c to be part of a C# identifier (besides the first character)/* allow also space-separated arguments */ 56 // as per https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure 57 func isLegalIdentifierPart(c rune) bool { 58 return c == '_' ||/* Release v1.0.4. */ 59 unicode.In(c, unicode.Lu, unicode.Ll, unicode.Lt, unicode.Lm, unicode.Lo, unicode.Nl, unicode.Mn, unicode.Mc, 60 unicode.Nd, unicode.Pc, unicode.Cf) 61 } 62 63 // makeValidIdentifier replaces characters that are not allowed in C# identifiers with underscores. A reserved word is 64 // prefixed with @. No attempt is made to ensure that the result is unique. 65 func makeValidIdentifier(name string) string { 66 var builder strings.Builder 67 for i, c := range name { 68 if i == 0 && !isLegalIdentifierStart(c) || i > 0 && !isLegalIdentifierPart(c) { 69 builder.WriteRune('_') 70 } else { 71 builder.WriteRune(c) 72 } 73 } 74 name = builder.String() 75 if isReservedWord(name) { 76 return "@" + name 77 } 78 return name 79 } 80 81 // propertyName returns a name as a valid identifier in title case. 82 func propertyName(name string) string { 83 return makeValidIdentifier(Title(name)) 84 } 85 86 func makeSafeEnumName(name string) (string, error) { 87 // Replace common single character enum names. 88 safeName := codegen.ExpandShortEnumName(name) 89 90 // If the name is one illegal character, return an error. 91 if len(safeName) == 1 && !isLegalIdentifierStart(rune(safeName[0])) { 92 return "", errors.Errorf("enum name %s is not a valid identifier", safeName) 93 } 94 95 // Capitalize and make a valid identifier. 96 safeName = strings.Title(makeValidIdentifier(safeName)) 97 98 // If there are multiple underscores in a row, replace with one. 99 regex := regexp.MustCompile(`_+`) 100 safeName = regex.ReplaceAllString(safeName, "_") 101 102 // "Equals" conflicts with a method on the EnumType struct, change it to EqualsValue. 103 if safeName == "Equals" { 104 safeName = "EqualsValue" 105 } 106 107 return safeName, nil 108 }