codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/EndpointDiscoveryGenerator.java
private static void generateEndpointDiscoveryHandler(
            Model model, SymbolProvider symbolProvider, GoWriter writer, ServiceShape service
    ) {
        if (!serviceSupportsEndpointDiscovery(model, service)) {
            return;
        }

        OperationShape discoveryOperation = model.expectShape(getEndpointDiscoveryOperationId(model, service),
                OperationShape.class);
        Symbol discoveryOperationSymbol = symbolProvider.toSymbol(discoveryOperation);
        Shape discoveryOperationInput = model.expectShape(discoveryOperation.getInput().get());
        Symbol discoveryOperationInputSymbol = symbolProvider.toSymbol(discoveryOperationInput);

        writer.addUseImports(SmithyGoDependency.CONTEXT);
        writer.openBlock("func (c *Client) $L(ctx context.Context, input $P, key string, opt $T) ($T, error) {", "}",
                DISCOVERY_ENDPOINT_HANDLER_NAME, discoveryOperationInputSymbol, DISCOVERY_ENDPOINT_OPTIONS,
                DISCOVERY_ENDPOINT_TYPE, () -> {

                    if (serviceSupportsCustomDiscoveryEndpoint(model, service)) {
                        // check if endpoint resolver for endpoint discovery is of service-specific endpoint resolver type
                        writer.writeDocs("assert endpoint resolver interface is of expected type.");
                        writer.write("endpointResolver, ok := opt.$L.($T)", ENDPOINT_RESOLVER_USED_FOR_DISCOVERY,
                                ENDPOINT_RESOLVER_INTERFACE_NAME);
                        writer.openBlock("if opt.$L != nil && !ok { return $T{},", "}",
                                ENDPOINT_RESOLVER_USED_FOR_DISCOVERY, DISCOVERY_ENDPOINT_TYPE, () -> {
                            writer.addUseImports(SmithyGoDependency.FMT);
                            writer.write("fmt.Errorf(\"Unexpected endpoint resolver type %T provided for endpoint "
                                            + "discovery api call\", opt.$L)",
                                    ENDPOINT_RESOLVER_USED_FOR_DISCOVERY);
                        });
                        writer.write("");
                    }

                    // fetch endpoint via making discovery call
                    writer.openBlock("output, err := c.$T(ctx, input, func(o *Options) {", "})",
                            discoveryOperationSymbol, () -> {
                                writer.write("o.EndpointOptions.$L = opt.$L", DISABLE_HTTPS, DISABLE_HTTPS);
                                writer.write("o.Logger = opt.Logger");

                                // if service supports custom discovery endpoint
                                if (serviceSupportsCustomDiscoveryEndpoint(model, service)) {
                                    writer.openBlock("if endpointResolver != nil {", "}", () -> {
                                        writer.write("o.EndpointResolver = endpointResolver");
                                    });
                                }
                            });
                    writer.write("if err != nil { return $T{}, err }", DISCOVERY_ENDPOINT_TYPE);
                    writer.write("");

                    // create an endpoint
                    writer.write("endpoint := $T{}", DISCOVERY_ENDPOINT_TYPE);
                    writer.write("endpoint.Key = key");
                    writer.write("");

                    writer.openBlock("for _, e := range output.Endpoints {", "}", () -> {
                        writer.write("if e.Address == nil { continue }");
                        writer.write("address := *e.Address");

                        writer.write("");
                        writer.write("var scheme string");
                        writer.addUseImports(SmithyGoDependency.STRINGS);
                        writer.openBlock("if idx := strings.Index(address, \"://\"); idx != -1 {", "}", () -> {
                            writer.write("scheme = address[:idx]");
                        });

                        writer.openBlock("if len(scheme) == 0 {", "}", () -> {
                            writer.write("scheme = \"https\"");
                            writer.openBlock("if opt.$L {", "}", DISABLE_HTTPS, () -> {
                                writer.write("scheme = \"http\"");
                            });
                            writer.addUseImports(SmithyGoDependency.FMT);
                            writer.write("address = fmt.Sprintf(\"%s://%s\", scheme, address)");
                        });

                        writer.write("");
                        writer.write("cachedInMinutes := e.CachePeriodInMinutes");
                        writer.addUseImports(SmithyGoDependency.NET_URL);
                        writer.write("u, err := url.Parse(address)");
                        writer.write("if err != nil { continue }");

                        writer.write("");
                        writer.openBlock("addr := $T {", "}", DISCOVERY_ENDPOINT_WEIGHTED_ADDRESS, () -> {
                            writer.write("URL: u,");
                            writer.addUseImports(SmithyGoDependency.TIME);
                            writer.write(
                                    "Expired: time.Now().Add(time.Duration(cachedInMinutes) * time.Minute).Round(0),");
                        });
                        writer.write("endpoint.Add(addr)");
                    });
                    writer.write("");
                    writer.write("c.$L.Add(endpoint)", CLIENT_MEMBER_ENDPOINT_CACHE);
                    writer.write("return endpoint, nil");
                });
    }
/**
     * Generates client scoped handler that attempts endpoint discovery from the service.
     *
     * @param model          is the generation model
     * @param symbolProvider is the symbol provider used in generation context
     * @param writer         is the GoWriter
     * @param service        is the service for which endpoint discovery is performed.
     */
Generates client scoped handler that attempts endpoint discovery from the service.
@param model          is the generation model
@param symbolProvider is the symbol provider used in generation context
@param writer         is the GoWriter
@param service        is the service for which endpoint discovery is performed.
Generates client scoped handler that attempts endpoint discovery from the service . @param model is the generation model @param symbolProvider is the symbol provider used in generation context @param writer is the GoWriter @param service is the service for which endpoint discovery is performed .
private static void generateEndpointDiscoveryHandler(
            Model model, SymbolProvider symbolProvider, GoWriter writer, ServiceShape service
    ) {
        if (!serviceSupportsEndpointDiscovery(model, service)) {
            return;
        }
        OperationShape discoveryOperation = model.expectShape(getEndpointDiscoveryOperationId(model, service),
                OperationShape.class);
        Symbol discoveryOperationSymbol = symbolProvider.toSymbol(discoveryOperation);
        Shape discoveryOperationInput = model.expectShape(discoveryOperation.getInput().get());
        Symbol discoveryOperationInputSymbol = symbolProvider.toSymbol(discoveryOperationInput);
        writer.addUseImports(SmithyGoDependency.CONTEXT);
        writer.openBlock("func (c *Client) $L(ctx context.Context, input $P, key string, opt $T) ($T, error) {", "}",
                DISCOVERY_ENDPOINT_HANDLER_NAME, discoveryOperationInputSymbol, DISCOVERY_ENDPOINT_OPTIONS,
                DISCOVERY_ENDPOINT_TYPE, () -> {
                    if (serviceSupportsCustomDiscoveryEndpoint(model, service)) {
                        writer.writeDocs("assert endpoint resolver interface is of expected type.");
                        writer.write("endpointResolver, ok := opt.$L.($T)", ENDPOINT_RESOLVER_USED_FOR_DISCOVERY,
                                ENDPOINT_RESOLVER_INTERFACE_NAME);
                        writer.openBlock("if opt.$L != nil && !ok { return $T{},", "}",
                                ENDPOINT_RESOLVER_USED_FOR_DISCOVERY, DISCOVERY_ENDPOINT_TYPE, () -> {
                            writer.addUseImports(SmithyGoDependency.FMT);
                            writer.write("fmt.Errorf(\"Unexpected endpoint resolver type %T provided for endpoint "
                                            + "discovery api call\", opt.$L)",
                                    ENDPOINT_RESOLVER_USED_FOR_DISCOVERY);
                        });
                        writer.write("");
                    }
                    writer.openBlock("output, err := c.$T(ctx, input, func(o *Options) {", "})",
                            discoveryOperationSymbol, () -> {
                                writer.write("o.EndpointOptions.$L = opt.$L", DISABLE_HTTPS, DISABLE_HTTPS);
                                writer.write("o.Logger = opt.Logger");
                                if (serviceSupportsCustomDiscoveryEndpoint(model, service)) {
                                    writer.openBlock("if endpointResolver != nil {", "}", () -> {
                                        writer.write("o.EndpointResolver = endpointResolver");
                                    });
                                }
                            });
                    writer.write("if err != nil { return $T{}, err }", DISCOVERY_ENDPOINT_TYPE);
                    writer.write("");
                    writer.write("endpoint := $T{}", DISCOVERY_ENDPOINT_TYPE);
                    writer.write("endpoint.Key = key");
                    writer.write("");
                    writer.openBlock("for _, e := range output.Endpoints {", "}", () -> {
                        writer.write("if e.Address == nil { continue }");
                        writer.write("address := *e.Address");
                        writer.write("");
                        writer.write("var scheme string");
                        writer.addUseImports(SmithyGoDependency.STRINGS);
                        writer.openBlock("if idx := strings.Index(address, \"://\"); idx != -1 {", "}", () -> {
                            writer.write("scheme = address[:idx]");
                        });
                        writer.openBlock("if len(scheme) == 0 {", "}", () -> {
                            writer.write("scheme = \"https\"");
                            writer.openBlock("if opt.$L {", "}", DISABLE_HTTPS, () -> {
                                writer.write("scheme = \"http\"");
                            });
                            writer.addUseImports(SmithyGoDependency.FMT);
                            writer.write("address = fmt.Sprintf(\"%s://%s\", scheme, address)");
                        });
                        writer.write("");
                        writer.write("cachedInMinutes := e.CachePeriodInMinutes");
                        writer.addUseImports(SmithyGoDependency.NET_URL);
                        writer.write("u, err := url.Parse(address)");
                        writer.write("if err != nil { continue }");
                        writer.write("");
                        writer.openBlock("addr := $T {", "}", DISCOVERY_ENDPOINT_WEIGHTED_ADDRESS, () -> {
                            writer.write("URL: u,");
                            writer.addUseImports(SmithyGoDependency.TIME);
                            writer.write(
                                    "Expired: time.Now().Add(time.Duration(cachedInMinutes) * time.Minute).Round(0),");
                        });
                        writer.write("endpoint.Add(addr)");
                    });
                    writer.write("");
                    writer.write("c.$L.Add(endpoint)", CLIENT_MEMBER_ENDPOINT_CACHE);
                    writer.write("return endpoint, nil");
                });
    }
private static void generateEndpointDiscoveryHandler ( Model model , SymbolProvider symbolProvider , GoWriter writer , ServiceShape service ) { if ( ! serviceSupportsEndpointDiscovery ( model , service ) ) { return ; } OperationShape discoveryOperation = model . expectShape ( getEndpointDiscoveryOperationId ( model , service ) , OperationShape . class ) ; Symbol discoveryOperationSymbol = symbolProvider . toSymbol ( discoveryOperation ) ; Shape discoveryOperationInput = model . expectShape ( discoveryOperation . getInput ( ) . get ( ) ) ; Symbol discoveryOperationInputSymbol = symbolProvider . toSymbol ( discoveryOperationInput ) ; writer . addUseImports ( SmithyGoDependency . CONTEXT ) ; writer . openBlock ( "func (c *Client) $L(ctx context.Context, input $P, key string, opt $T) ($T, error) {" , "}" , DISCOVERY_ENDPOINT_HANDLER_NAME , discoveryOperationInputSymbol , DISCOVERY_ENDPOINT_OPTIONS , DISCOVERY_ENDPOINT_TYPE , ( ) -> { if ( serviceSupportsCustomDiscoveryEndpoint ( model , service ) ) { writer . writeDocs ( "assert endpoint resolver interface is of expected type." ) ; writer . write ( "endpointResolver, ok := opt.$L.($T)" , ENDPOINT_RESOLVER_USED_FOR_DISCOVERY , ENDPOINT_RESOLVER_INTERFACE_NAME ) ; writer . openBlock ( "if opt.$L != nil && !ok { return $T{}," , "}" , ENDPOINT_RESOLVER_USED_FOR_DISCOVERY , DISCOVERY_ENDPOINT_TYPE , ( ) -> { writer . addUseImports ( SmithyGoDependency . FMT ) ; writer . write ( "fmt.Errorf(\"Unexpected endpoint resolver type %T provided for endpoint " + "discovery api call\", opt.$L)" , ENDPOINT_RESOLVER_USED_FOR_DISCOVERY ) ; } ) ; writer . write ( "" ) ; } writer . openBlock ( "output, err := c.$T(ctx, input, func(o *Options) {" , "})" , discoveryOperationSymbol , ( ) -> { writer . write ( "o.EndpointOptions.$L = opt.$L" , DISABLE_HTTPS , DISABLE_HTTPS ) ; writer . write ( "o.Logger = opt.Logger" ) ; if ( serviceSupportsCustomDiscoveryEndpoint ( model , service ) ) { writer . openBlock ( "if endpointResolver != nil {" , "}" , ( ) -> { writer . write ( "o.EndpointResolver = endpointResolver" ) ; } ) ; } } ) ; writer . write ( "if err != nil { return $T{}, err }" , DISCOVERY_ENDPOINT_TYPE ) ; writer . write ( "" ) ; writer . write ( "endpoint := $T{}" , DISCOVERY_ENDPOINT_TYPE ) ; writer . write ( "endpoint.Key = key" ) ; writer . write ( "" ) ; writer . openBlock ( "for _, e := range output.Endpoints {" , "}" , ( ) -> { writer . write ( "if e.Address == nil { continue }" ) ; writer . write ( "address := *e.Address" ) ; writer . write ( "" ) ; writer . write ( "var scheme string" ) ; writer . addUseImports ( SmithyGoDependency . STRINGS ) ; writer . openBlock ( "if idx := strings.Index(address, \"://\"); idx != -1 {" , "}" , ( ) -> { writer . write ( "scheme = address[:idx]" ) ; } ) ; writer . openBlock ( "if len(scheme) == 0 {" , "}" , ( ) -> { writer . write ( "scheme = \"https\"" ) ; writer . openBlock ( "if opt.$L {" , "}" , DISABLE_HTTPS , ( ) -> { writer . write ( "scheme = \"http\"" ) ; } ) ; writer . addUseImports ( SmithyGoDependency . FMT ) ; writer . write ( "address = fmt.Sprintf(\"%s://%s\", scheme, address)" ) ; } ) ; writer . write ( "" ) ; writer . write ( "cachedInMinutes := e.CachePeriodInMinutes" ) ; writer . addUseImports ( SmithyGoDependency . NET_URL ) ; writer . write ( "u, err := url.Parse(address)" ) ; writer . write ( "if err != nil { continue }" ) ; writer . write ( "" ) ; writer . openBlock ( "addr := $T {" , "}" , DISCOVERY_ENDPOINT_WEIGHTED_ADDRESS , ( ) -> { writer . write ( "URL: u," ) ; writer . addUseImports ( SmithyGoDependency . TIME ) ; writer . write ( "Expired: time.Now().Add(time.Duration(cachedInMinutes) * time.Minute).Round(0)," ) ; } ) ; writer . write ( "endpoint.Add(addr)" ) ; } ) ; writer . write ( "" ) ; writer . write ( "c.$L.Add(endpoint)" , CLIENT_MEMBER_ENDPOINT_CACHE ) ; writer . write ( "return endpoint, nil" ) ; } ) ; }
Generates client scoped handler that attempts endpoint discovery from the service.
Generates client scoped handler that attempts endpoint discovery from the service .
// check if endpoint resolver for endpoint discovery is of service-specific endpoint resolver type // fetch endpoint via making discovery call // if service supports custom discovery endpoint // create an endpoint