private static void TransformMethods(CodeModelGo cmg) { foreach (var mg in cmg.MethodGroups) { mg.Transform(cmg); } var wrapperTypes = new Dictionary <string, CompositeTypeGo>(); foreach (var method in cmg.Methods.Cast <MethodGo>()) { method.Transform(cmg); var scope = new VariableScopeProvider(); foreach (var parameter in method.Parameters) { parameter.Name = scope.GetVariableName(parameter.Name); } // fix up method return types if (method.ReturnType.Body.ShouldBeSyntheticType()) { var ctg = new CompositeTypeGo(method.ReturnType.Body); if (wrapperTypes.ContainsKey(ctg.Name)) { method.ReturnType = new Response(wrapperTypes[ctg.Name], method.ReturnType.Headers); } else { wrapperTypes.Add(ctg.Name, ctg); cmg.Add(ctg); method.ReturnType = new Response(ctg, method.ReturnType.Headers); } } if (method.IsPageable && !method.IsNextMethod) { // for pageable methods replace the return type with a page iterator. // do this before LROs as you can have pageable operations that are // long-running and for this case we want the future to return a paged type. // note that we don't want to do this for the "next methods" that are // defined explicitly in swagger as they will be used in lieu of // generating a custom preparer so they must return the underlying type. cmg.CreatePageableTypeForMethod(method); } if (method.IsLongRunningOperation()) { // for LROs we replace the return type with a future that // knows how to poll for the operation's status and result cmg.CreateFutureTypeForMethod(method); } } }
/// <summary> /// Creates a string from the first letter in each word. /// E.g. "SomeTypeName" would generate the string "stn". /// </summary> /// <param name="scope">Provide an instance to ensure variable names are unique within a given scope.</param> public static string ToVariableName(this string longName, VariableScopeProvider scope = null) { var initials = from word in longName.ToWords() select word[0]; var acronym = string.Concat(initials).ToLowerInvariant(); var name = CodeNamerGo.Instance.GetVariableName(acronym); if (scope != null) { name = scope.GetVariableName(name); } return(name); }
private void TransformMethods(CodeModelGo cmg) { foreach (var mg in cmg.MethodGroups) { mg.Transform(cmg); } var wrapperTypes = new Dictionary <string, CompositeTypeGo>(); foreach (var method in cmg.Methods) { ((MethodGo)method).Transform(cmg); var scope = new VariableScopeProvider(); foreach (var parameter in method.Parameters) { parameter.Name = scope.GetVariableName(parameter.Name); } // fix up method return types if (method.ReturnType.Body.ShouldBeSyntheticType()) { var ctg = new CompositeTypeGo(method.ReturnType.Body); if (wrapperTypes.ContainsKey(ctg.Name)) { method.ReturnType = new Response(wrapperTypes[ctg.Name], method.ReturnType.Headers); } else { wrapperTypes.Add(ctg.Name, ctg); cmg.Add(ctg); method.ReturnType = new Response(ctg, method.ReturnType.Headers); } } } }
/// <summary> /// Formats a string for naming a local variable using Camel case by default. /// </summary> /// <param name="name"></param> /// <param name="scope">Used to ensure variable names are unique within a given scope.</param> /// <returns>The formatted string.</returns> public string GetVariableName(string name, VariableScopeProvider scope) => scope.GetVariableName(GetVariableName(name));
public override void NormalizeClientModel(ServiceClient client) { PackageName = PackageNameFromNamespace(client.Namespace); base.NormalizeClientModel(client); List <SyntheticType> syntheticTypes = new List <SyntheticType>(); // Trim the package name from exported types; append a suitable qualifier, if needed, to avoid conflicts. var exportedTypes = new HashSet <object>(); exportedTypes.UnionWith(client.EnumTypes); exportedTypes.UnionWith(client.Methods); exportedTypes.UnionWith(client.ModelTypes); var stutteringTypes = exportedTypes .Where(exported => (exported is IType && (exported as IType).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase)) || (exported is Method && (exported as Method).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase))); if (stutteringTypes.Count() > 0) { Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.NamesStutter, stutteringTypes.Count())); stutteringTypes .ToList().ForEach(exported => { var name = exported is IType ? (exported as IType).Name : (exported as Method).Name; Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.StutteringName, name)); name = name.TrimPackageName(PackageName); var nameInUse = exportedTypes .Any(et => (et is IType && (et as IType).Name.Equals(name)) || (et is Method && (et as Method).Name.Equals(name))); if (exported is EnumType) { (exported as EnumType).Name = AttachTypeName(name, PackageName, nameInUse, "Enum"); } else if (exported is CompositeType) { (exported as CompositeType).Name = AttachTypeName(name, PackageName, nameInUse, "Type"); } else if (exported is Method) { (exported as Method).Name = AttachTypeName(name, PackageName, nameInUse, "Method"); } }); } foreach (var method in client.Methods) { var scope = new VariableScopeProvider(); foreach (var parameter in method.Parameters) { parameter.Name = scope.GetVariableName(parameter.Name); } if (SyntheticType.ShouldBeSyntheticType(method.ReturnType.Body)) { SyntheticType st = new SyntheticType(method.ReturnType.Body); if (syntheticTypes.Contains(st)) { method.ReturnType = new Response(syntheticTypes.Find(i => i.Equals(st)), method.ReturnType.Headers); } else { syntheticTypes.Add(st); client.ModelTypes.Add(st); method.ReturnType = new Response(st, method.ReturnType.Headers); } } } normalizedTypesForUserDefinedNames(client); }
public override void NormalizeClientModel(ServiceClient client) { PackageName = PackageNameFromNamespace(client.Namespace); base.NormalizeClientModel(client); List<SyntheticType> syntheticTypes = new List<SyntheticType>(); // Trim the package name from exported types; append a suitable qualifier, if needed, to avoid conflicts. var exportedTypes = new HashSet<object>(); exportedTypes.UnionWith(client.EnumTypes); exportedTypes.UnionWith(client.Methods); exportedTypes.UnionWith(client.ModelTypes); var stutteringTypes = exportedTypes .Where(exported => (exported is IType && (exported as IType).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase)) || (exported is Method && (exported as Method).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase))); if (stutteringTypes.Count() > 0) { Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.NamesStutter, stutteringTypes.Count())); stutteringTypes .ToList().ForEach(exported => { var name = exported is IType ? (exported as IType).Name : (exported as Method).Name; Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.StutteringName, name)); name = name.TrimPackageName(PackageName); var nameInUse = exportedTypes .Any(et => (et is IType && (et as IType).Name.Equals(name)) || (et is Method && (et as Method).Name.Equals(name))); if (exported is EnumType) { (exported as EnumType).Name = AttachTypeName(name, PackageName, nameInUse, "Enum"); } else if (exported is CompositeType) { (exported as CompositeType).Name = AttachTypeName(name, PackageName, nameInUse, "Type"); } else if (exported is Method) { (exported as Method).Name = AttachTypeName(name, PackageName, nameInUse, "Method"); } }); } foreach (var method in client.Methods) { var scope = new VariableScopeProvider(); foreach (var parameter in method.Parameters) { parameter.Name = scope.GetVariableName(parameter.Name); } if (SyntheticType.ShouldBeSyntheticType(method.ReturnType.Body)) { SyntheticType st = new SyntheticType(method.ReturnType.Body); if (syntheticTypes.Contains(st)) { method.ReturnType = new Response(syntheticTypes.Find(i => i.Equals(st)), method.ReturnType.Headers); } else { syntheticTypes.Add(st); client.ModelTypes.Add(st); method.ReturnType = new Response(st, method.ReturnType.Headers); } } } normalizedTypesForUserDefinedNames(client); }
/// <summary> /// Creates a string from the first letter in each word. /// E.g. "SomeTypeName" would generate the string "stn". /// </summary> /// <param name="scope">Provide an instance to ensure variable names are unique within a given scope.</param> public static string ToVariableName(this Fixable <string> longName, VariableScopeProvider scope = null) { return(longName.ToString().ToVariableName(scope)); }