コード例 #1
0
        public PackageModel(RestDescription discoveryDoc, Features features, PackageEnumStorage enumStorage)
        {
            _features     = features;
            _discoveryDoc = discoveryDoc;
            ApiName       = discoveryDoc.Name;

            // Note that spaces are removed first, because the Python code doesn't Pascal-case them. For example,
            // the "Cloud Memorystore for Memcached" API has a package name of "CloudMemorystoreforMemcached".
            // It's awful, but that's the way it works...
            ClassName  = (discoveryDoc.CanonicalName ?? discoveryDoc.Name).Replace(" ", "").ToMemberName();
            ApiVersion = discoveryDoc.Version;
            string versionNoDots        = discoveryDoc.Version.Replace('.', '_');
            var    camelizedPackagePath = discoveryDoc.PackagePath is null
                ? ""
                : string.Join('.', discoveryDoc.PackagePath.Split('/').Select(part => part.ToUpperCamelCase())) + ".";

            PackageName = $"Google.Apis.{camelizedPackagePath}{ClassName}.{versionNoDots}";
            DataModels  = discoveryDoc.Schemas.ToReadOnlyList(pair => new DataModel(this, parent: null, name: pair.Key, schema: pair.Value));

            // Populate the data model dictionary early, as methods and resources refer to the data model types.
            foreach (var dm in DataModels)
            {
                _dataModelsById[dm.Id] = dm;
            }
            Resources = discoveryDoc.Resources.ToReadOnlyList(pair => new ResourceModel(this, parent: null, pair.Key, pair.Value));
            // TODO: Ordering?
            AuthScopes = (discoveryDoc.Auth?.Oauth2?.Scopes).ToReadOnlyList(pair => new AuthScope(pair.Key, pair.Value.Description));
            ServiceTyp = Typ.Manual(PackageName, $"{ClassName}Service");
            GenericBaseRequestTypDef = Typ.Manual(PackageName, $"{ClassName}BaseServiceRequest");
            Methods            = discoveryDoc.Methods.ToReadOnlyList(pair => new MethodModel(this, null, pair.Key, pair.Value));
            PackageEnumStorage = enumStorage;
        }
コード例 #2
0
        internal static void TestOutput(string directory, bool ignoreCsProj = false, bool ignoreConfig = false)
        {
            var resourceDirectory = Path.Combine(TestDirectory, "GoldenTestData", directory);
            var json     = File.ReadAllText(Path.Combine(resourceDirectory, "discovery.json"));
            var features = new Features
            {
                ReleaseVersion        = "1.49.0",
                CurrentSupportVersion = "1.49.0",
                Net40SupportVersion   = "1.25.0",
                PclSupportVersion     = "1.10.0"
            };
            PackageEnumStorage enumStorage = PackageEnumStorage.FromJson("{}");
            var files = CodeGenerator.Generate(json, features, enumStorage).ToList();

            // Check output is present.
            Assert.NotEmpty(files);

            foreach (var file in files)
            {
                if ((ignoreCsProj && file.RelativePath.EndsWith(".csproj")) ||
                    (ignoreConfig && file.RelativePath.EndsWith(".config")))
                {
                    continue;
                }
                var expectedFilePath = Path.Combine(TestDirectory, "GoldenTestData", directory, file.RelativePath);

                TextComparer.CompareText(expectedFilePath, file);
            }

            // TODO: Validate enum storage
        }
コード例 #3
0
        public void KeysAreIndependent()
        {
            string key1 = Guid.NewGuid().ToString();
            string key2 = Guid.NewGuid().ToString();

            var storage = PackageEnumStorage.FromJson("{}");

            Assert.Equal(0, storage.GetOrAddEnumValue(key1, "x"));
            Assert.Equal(1, storage.GetOrAddEnumValue(key1, "y"));

            // The "y" value from key 1 shouldn't affect key 2
            Assert.Equal(0, storage.GetOrAddEnumValue(key2, "y"));
            Assert.Equal(1, storage.GetOrAddEnumValue(key2, "z"));

            // Back to key 1, where the "z" value from key 2 shouldn't
            // affect it
            Assert.Equal(2, storage.GetOrAddEnumValue(key1, "z"));
        }
コード例 #4
0
        public void SimulatedOrderingChange()
        {
            string key = Guid.NewGuid().ToString();

            // First run: values [ "zero", "one", "two" ]
            var storage = PackageEnumStorage.FromJson("{}");

            Assert.Equal(0, storage.GetOrAddEnumValue(key, "zero"));
            Assert.Equal(1, storage.GetOrAddEnumValue(key, "one"));
            Assert.Equal(2, storage.GetOrAddEnumValue(key, "two"));
            string json = storage.ToJson();

            // Second run: values [ "two", "zero", "one", "three" ]
            storage = PackageEnumStorage.FromJson(json);
            Assert.Equal(2, storage.GetOrAddEnumValue(key, "two"));
            Assert.Equal(0, storage.GetOrAddEnumValue(key, "zero"));
            Assert.Equal(1, storage.GetOrAddEnumValue(key, "one"));
            Assert.Equal(3, storage.GetOrAddEnumValue(key, "three"));
        }