OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright 2010 Google Inc | |
3 | |
4 Licensed under the Apache License, Version 2.0 (the "License"); | |
5 you may not use this file except in compliance with the License. | |
6 You may obtain a copy of the License at | |
7 | |
8 http://www.apache.org/licenses/LICENSE-2.0 | |
9 | |
10 Unless required by applicable law or agreed to in writing, software | |
11 distributed under the License is distributed on an "AS IS" BASIS, | |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 See the License for the specific language governing permissions and | |
14 limitations under the License. | |
15 */ | |
16 | |
17 using System; | |
18 using System.CodeDom; | |
19 using System.Collections.Generic; | |
20 using Google.Apis.Discovery; | |
21 using Google.Apis.Logging; | |
22 using Google.Apis.Testing; | |
23 using Google.Apis.Tools.CodeGen.Generator; | |
24 | |
25 namespace Google.Apis.Tools.CodeGen.Decorator.ResourceDecorator | |
26 { | |
27 /// <summary> | |
28 /// Creates a constructor for this resource, this is a standard decorator so
this either needs to be· | |
29 /// present or another decorator that creates a constructor with the same si
gnature put in. | |
30 /// </summary> | |
31 /// <remarks>This decorator will not be applied to the service-resource.</re
marks> | |
32 public class StandardConstructorResourceDecorator : IResourceDecorator | |
33 { | |
34 private static readonly ILogger logger = | |
35 ApplicationContext.Logger.ForType<StandardConstructorResourceDecorat
or>(); | |
36 | |
37 #region IResourceDecorator Members | |
38 | |
39 public void DecorateClass(IResource resource, | |
40 string className, | |
41 CodeTypeDeclaration resourceClass, | |
42 ResourceClassGenerator generator, | |
43 string serviceClassName, | |
44 IEnumerable<IResourceDecorator> allDecorators) | |
45 { | |
46 if (!resource.IsServiceResource) // Only add this constructor to non
-service resources. | |
47 { | |
48 logger.Debug("Adding standard constructor to Resource[{0}]", res
ource.Name); | |
49 resourceClass.Members.Add(CreateConstructor(serviceClassName, re
source)); | |
50 } | |
51 } | |
52 | |
53 | |
54 public void DecorateMethodBeforeExecute(IResource resource, IMethod meth
od, CodeMemberMethod codeMember) { } | |
55 | |
56 | |
57 public void DecorateMethodAfterExecute(IResource resource, IMethod metho
d, CodeMemberMethod codeMember) { } | |
58 | |
59 #endregion | |
60 | |
61 [VisibleForTestOnly] | |
62 internal CodeConstructor CreateConstructor(String serviceClassName, IRes
ource resource) | |
63 { | |
64 var constructor = new CodeConstructor(); | |
65 | |
66 // public [ResourceClass]([ServiceClass] service, Google.Apis.Authen
tication.IAuthenticator authenticator) | |
67 constructor.Attributes = MemberAttributes.Public; | |
68 constructor.Parameters.Add( | |
69 new CodeParameterDeclarationExpression(serviceClassName, Resourc
eBaseGenerator.ServiceFieldName)); | |
70 constructor.Parameters.Add( | |
71 new CodeParameterDeclarationExpression(typeof(Google.Apis.Authen
tication.IAuthenticator), | |
72 ServiceClassGenerator.AuthenticatorName)); | |
73 | |
74 // this.service = service | |
75 constructor.Statements.Add( | |
76 new CodeAssignStatement( | |
77 new CodeFieldReferenceExpression( | |
78 new CodeThisReferenceExpression(), ResourceBaseGenerator
.ServiceFieldName), | |
79 new CodeArgumentReferenceExpression(ResourceBaseGenerator.Se
rviceFieldName))); | |
80 | |
81 // this.authenticator = authenticator | |
82 constructor.Statements.Add( | |
83 new CodeAssignStatement( | |
84 new CodeFieldReferenceExpression( | |
85 new CodeThisReferenceExpression(), ServiceClassGenerator
.AuthenticatorName), | |
86 new CodeArgumentReferenceExpression(ServiceClassGenerator.Au
thenticatorName))); | |
87 | |
88 // Initialize subresources | |
89 constructor.Statements.AddRange(CreateSubresourceCreateStatements(re
source)); | |
90 | |
91 return constructor; | |
92 } | |
93 | |
94 [VisibleForTestOnly] | |
95 internal CodeStatementCollection CreateSubresourceCreateStatements(IReso
urce resource) | |
96 { | |
97 var initializers = new CodeStatementCollection(); | |
98 ICollection<string> otherResourceNames = resource.Resources.Keys; | |
99 | |
100 foreach (IResource subresource in resource.Resources.Values) | |
101 { | |
102 IEnumerable<string> relevantResourceNames = otherResourceNames.W
ithout(subresource.Name); | |
103 | |
104 // Retrieve backing field name and type of the (already created)
subresource | |
105 var fieldRef = ServiceClassGenerator.GetFieldReference(subresour
ce, relevantResourceNames); | |
106 var fieldType = GeneratorUtils.GetClassName(subresource, relevan
tResourceNames); | |
107 | |
108 // ... new SubResource(service); | |
109 var fieldConstructor = new CodeObjectCreateExpression(fieldType)
; | |
110 fieldConstructor.Parameters.Add( | |
111 new CodeVariableReferenceExpression(ResourceBaseGenerator.Se
rviceFieldName)); | |
112 fieldConstructor.Parameters.Add( | |
113 new CodeVariableReferenceExpression(ServiceClassGenerator.Au
thenticatorName)); | |
114 | |
115 // subResource = ... | |
116 var assign = new CodeAssignStatement(fieldRef, fieldConstructor)
; | |
117 initializers.Add(assign); | |
118 } | |
119 | |
120 return initializers; | |
121 } | |
122 } | |
123 } | |
OLD | NEW |