1 // Copyright 2020 Google LLC 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. 5 // You may obtain a copy of the License at 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. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.api.generator.gapic.model; 16 17 import com.google.auto.value.AutoValue; 18 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.ImmutableMap; 20 import com.google.gapic.metadata.GapicMetadata; 21 import java.util.Collections; 22 import java.util.List; 23 import java.util.Map; 24 import java.util.Set; 25 import java.util.TreeMap; 26 import java.util.stream.Collectors; 27 import javax.annotation.Nullable; 28 29 @AutoValue 30 public abstract class GapicContext { 31 // Keep a non-AutoValue reference to GapicMetadata, since we need to update 32 // it iteratively as we generate client methods. 33 private GapicMetadata gapicMetadata = defaultGapicMetadata(); 34 35 // Maps the message name (as it appears in the protobuf) to Messages. messages()36 public abstract ImmutableMap<String, Message> messages(); 37 38 // Maps the resource type string to ResourceNames. resourceNames()39 public abstract ImmutableMap<String, ResourceName> resourceNames(); 40 services()41 public abstract ImmutableList<Service> services(); 42 43 // Ensures ordering for deterministic tests. mixinServices()44 public abstract ImmutableList<Service> mixinServices(); 45 helperResourceNames()46 public abstract ImmutableMap<String, ResourceName> helperResourceNames(); 47 gapicMetadataEnabled()48 public abstract boolean gapicMetadataEnabled(); 49 restNumericEnumsEnabled()50 public abstract boolean restNumericEnumsEnabled(); 51 gapicMetadata()52 public GapicMetadata gapicMetadata() { 53 return gapicMetadata; 54 } 55 56 @Nullable serviceConfig()57 public abstract GapicServiceConfig serviceConfig(); 58 59 @Nullable serviceYamlProto()60 public abstract com.google.api.Service serviceYamlProto(); 61 hasServiceYamlProto()62 public boolean hasServiceYamlProto() { 63 return serviceYamlProto() != null; 64 } 65 updateGapicMetadata(GapicMetadata newMetadata)66 public void updateGapicMetadata(GapicMetadata newMetadata) { 67 gapicMetadata = newMetadata; 68 } 69 defaultGapicMetadata()70 static GapicMetadata defaultGapicMetadata() { 71 return GapicMetadata.newBuilder() 72 .setSchema("1.0") 73 .setComment( 74 "This file maps proto services/RPCs to the corresponding library clients/methods") 75 .setLanguage("java") 76 .build(); 77 } 78 toBuilder()79 public abstract Builder toBuilder(); 80 transport()81 public abstract Transport transport(); 82 builder()83 public static Builder builder() { 84 return new AutoValue_GapicContext.Builder() 85 .setMixinServices(Collections.emptyList()) 86 .setGapicMetadataEnabled(false) 87 .setRestNumericEnumsEnabled(false); 88 } 89 90 @AutoValue.Builder 91 public abstract static class Builder { setMessages(Map<String, Message> messages)92 public abstract Builder setMessages(Map<String, Message> messages); 93 setResourceNames(Map<String, ResourceName> resourceNames)94 public abstract Builder setResourceNames(Map<String, ResourceName> resourceNames); 95 setServices(List<Service> services)96 public abstract Builder setServices(List<Service> services); 97 setMixinServices(List<Service> mixinServices)98 public abstract Builder setMixinServices(List<Service> mixinServices); 99 setHelperResourceNames(Set<ResourceName> helperResourceNames)100 public Builder setHelperResourceNames(Set<ResourceName> helperResourceNames) { 101 return setHelperResourceNames( 102 helperResourceNames.stream() 103 .collect(Collectors.toMap(r -> r.resourceTypeString(), r -> r))); 104 } 105 setHelperResourceNames(Map<String, ResourceName> helperResourceNames)106 abstract Builder setHelperResourceNames(Map<String, ResourceName> helperResourceNames); 107 setServiceConfig(GapicServiceConfig serviceConfig)108 public abstract Builder setServiceConfig(GapicServiceConfig serviceConfig); 109 setServiceYamlProto(com.google.api.Service serviceYamlProto)110 public abstract Builder setServiceYamlProto(com.google.api.Service serviceYamlProto); 111 setGapicMetadataEnabled(boolean gapicMetadataEnabled)112 public abstract Builder setGapicMetadataEnabled(boolean gapicMetadataEnabled); 113 setRestNumericEnumsEnabled(boolean restNumericEnumsEnabled)114 public abstract Builder setRestNumericEnumsEnabled(boolean restNumericEnumsEnabled); 115 setTransport(Transport transport)116 public abstract Builder setTransport(Transport transport); 117 resourceNames()118 abstract ImmutableMap<String, ResourceName> resourceNames(); 119 helperResourceNames()120 abstract ImmutableMap<String, ResourceName> helperResourceNames(); 121 autoBuild()122 abstract GapicContext autoBuild(); 123 build()124 public GapicContext build() { 125 setResourceNames(new TreeMap<>(resourceNames())); 126 setHelperResourceNames(new TreeMap<>(helperResourceNames())); 127 return autoBuild(); 128 } 129 } 130 } 131