Giter Club home page Giter Club logo

Comments (2)

subfuzion avatar subfuzion commented on May 28, 2024

Not a bug, will close this issue and reopen another as a feature enhancement to be able to parameterize @cloudfunction for different HTTP verbs or alternatively to recognize a specific function naming convention (handlePost, etc.)

from functions-framework-dart.

subfuzion avatar subfuzion commented on May 28, 2024

Scratch that, reopening because this does appear in fact to be problematic.

Overview

In the current implementation the @cloudfunction() annotation maps an external target name to an internal function.

Using the @cloudfunction() annotation without an argument maps function as the default external target name to an internal function.

When providing a name as an argument to @cloudfunction() to override the default name function, then this name must also be set corresponding FUNCTION_TARGET environment variable or --target command line option, as shown below.

Default function

In the following example, the @cloudfunction external target default name is function, which will be mapped to the internal handleGet function. Since function is the default external target name, it isn't necessary to set the FUNCTION_TARGET environment variable or --target command line option.

// default `function` is mapped to `handleGet`
@CloudFunction()
Response handleGet(Request request) => Response.ok('Hello, World!');

Overridding the default

In the next example, the @cloudfunction external target name is explicitly set to foo, which will be mapped to the internal bar function. Since foo overrides the default external target name function, either the FUNCTION_TARGET environment variable or --target command line option must be set to foo.

@CloudFunction('foo')
Response bar(Request request) => Response.ok('Hello, World!');

To invoke foo, either of the following would work:

FUNCTION_TARGET=foo dart run bin/main.dart

or

dart run bin/main.dart --target=foo

The problem

The problem with the current implementation is twofold:

  1. The need for target mapping is not obvious and somewhat confusing.
  2. There can only be one function annotated with @cloudfunction. The expectation would be that there could be several declarations.

Consider the following:

// handle HTTP GET requests by convention
@CloudFunction()
Response handleGet(Request request) { ... }

// handle HTTP POST requests by convention
@CloudFunction()
Response handlePost(Request request) { ... }

// handle request for conformance testing
@CloudFunction()
Response writeHttp(Request request) { /* write `request.body` contents to file: `function_output.json` */ }

To invoke writeHttp for conformance testing, either of the following should work:

FUNCTION_TARGET=writeHttp dart run bin/main.dart

or

dart run bin/main.dart --target=writeHttp

Resolution

Resolving this requires a bit more thought around the desired developer experience. The current implementation works as originally designed, so I'm closing this issue (again) and will open another issue with a proposal for an updated approach to better match expectations.

from functions-framework-dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.