forked from googleapis/gapic-generator-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaming.cs
More file actions
81 lines (75 loc) · 3.5 KB
/
Naming.cs
File metadata and controls
81 lines (75 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.Api.Generator.Utils;
using Google.Api.Generator.Utils.Roslyn;
namespace Google.Api.Generator.Rest.Models
{
/// <summary>
/// Centralized place for naming rules within the REST generator.
/// </summary>
internal static class Naming
{
/// <summary>
/// Creates a member name from the given name in the discovery doc.
/// This is generally just the upper-camel-case version of the name,
/// but with some tweaks.
/// </summary>
internal static string ToMemberName(this string name, bool addUnderscoresToEscape = true)
{
if (char.IsDigit(name[0]))
{
name = "Value" + name;
}
var upper = name.ToUpperCamelCase(upperAfterDigit: null);
// We don't really need to escape keywords given that we've upper-cased it,
// but this is what the Python code does.
if (addUnderscoresToEscape && (Keywords.IsKeyword(upper.ToLowerInvariant()) || Keywords.IsReservedName(upper)))
{
upper += "__";
}
return upper;
}
/// <summary>
/// Equivalent to ToClassName in csharp_generator.py
/// </summary>
internal static string ToClassName(this string name, PackageModel package, string containingClass, bool escapeIfKeyword = true)
{
if (escapeIfKeyword && Keywords.IsKeyword(name.ToLowerInvariant()))
{
return package.ClassName.ToUpperCamelCase() + name.ToUpperCamelCase();
}
// csharp_generator.py splits by dots and then joins the bits together with dots,
// but we never need that (and it causes issues with names like "$.xgafv").
var candidate = ToMemberName(name, addUnderscoresToEscape: escapeIfKeyword);
var suffix = candidate == containingClass ? "Schema" : "";
return candidate + suffix;
}
internal static string ToAnonymousModelClassName(this string name, PackageModel package, string containingClass, bool inAdditionalProperties)
{
// This is ugly, but when we've got an anonymous model that's also a dictionary, we end up with things round the
// wrong way otherwise :( It's hard to fix the recursion to get this in the right order.
name = inAdditionalProperties ? name[..^7] + "DataElement" : name + "Data";
return name.ToClassName(package, containingClass);
}
/// <summary>
/// Creates a name suitable for a local variable or parameter, using the package name
/// as a prefix if necessary.
/// </summary>
internal static string ToLocalVariableName(this string name, PackageModel package)
{
string lowerCamel = name.ToLowerCamelCase();
return Keywords.IsKeyword(lowerCamel) ? package.ApiName + name.ToUpperCamelCase() : lowerCamel;
}
}
}