sdk-generator icon indicating copy to clipboard operation
sdk-generator copied to clipboard

Add GO Templates to the SDK Generator

Open eldadfux opened this issue 6 years ago • 21 comments

We need help adding GO templates to this project to allow easy generation of GO SDKs for Appwrite server.

A full guide on how to add new language support to the Appwrite SDK generator is located here: https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md

eldadfux avatar Sep 29 '19 17:09 eldadfux

Hi @eldadfux ,

I'd like to do this task.

panz3r avatar Sep 30 '19 23:09 panz3r

Hey @panz3r that will be great. Let me know if you need any help with it. Also, feel free to reach out on our Gitter community or just DM me!

https://gitter.im/appwrite/community

eldadfux avatar Oct 01 '19 04:10 eldadfux

@panz3r assigned you to this issue, please let me know if you need any help.

eldadfux avatar Oct 02 '19 17:10 eldadfux

@panz3r, I finally had some time to play around with the Go implementation. I have generate a demo SDK using the Appwrite API spec in this demo repo: https://github.com/aw-tests/sdk-for-go

When I run this example code:

package main

import (
    "fmt"
    "os"
    "github.com/aw-tests/sdk-for-go"
)

func main() {
    var client := appwrite.Client{}

    client.SetProject("")
    client.SetKey("")

    var service := appwrite.Avatars{
        client: &client
    }

    var response, error := service.GetBrowser("aa")

    if error != nil {
        panic(error)
    }

    fmt.Println(response)
}

I get the following errors:

go: finding github.com/aw-tests/sdk-for-go latest
go: downloading github.com/aw-tests/sdk-for-go v0.0.0-20200128164133-02ad4443fc25
go: extracting github.com/aw-tests/sdk-for-go v0.0.0-20200128164133-02ad4443fc25
# command-line-arguments
./main.go:10:16: syntax error: unexpected :=, expecting =
./main.go:15:17: syntax error: unexpected :=, expecting =
./main.go:16:24: syntax error: unexpected newline, expecting comma or }
./main.go:19:25: syntax error: unexpected :=, expecting =

Any idea how can we fix this?

eldadfux avatar Jan 28 '20 18:01 eldadfux

Hi @eldadfux ,

package main

import (
    "fmt"
    "os"
    "github.com/aw-tests/sdk-for-go"
)

func main() {
    var client := appwrite.Client{}

This line should be

    client := appwrite.Client{}
    client.SetProject("")
    client.SetKey("")

    var service := appwrite.Avatars{

This one should be:

 service := appwrite.Avatars{

but there's an issue with the generated SDK, since client is a private field of the Avatars struct it cannot be set this way, to fix this there are 2 ways:

  • Add a method New{{ServiceName}}Service(clt *Client) to the generated SDK
  • make the client field public

I'd advice for the first one. The method would look like this

func NewAvatars(clt *Client) *Avatars {
	return &Avatars{
		client: clt,
	}
}
        client: &client
    }

    var response, error := service.GetBrowser("aa")

This one also should be

response, error := service.GetBrowser("aa", 100, 100, 80)

N.B: All 4 params are required.

    if error != nil {
        panic(error)
    }

    fmt.Println(response)
}

panz3r avatar Jan 28 '20 20:01 panz3r

@eldadfux Can I work on this?

Suven-p avatar Apr 16 '23 07:04 Suven-p

@Suven-p Just do it. I implemented some more functions. One big thing that is missing, is the http streaming client support.

phaus avatar Apr 16 '23 08:04 phaus

Where is streaming used?

Suven-p avatar Apr 16 '23 11:04 Suven-p

Ah… not streaming. I mean chunked/resumable uploads. See https://github.com/appwrite/sdk-generator/pull/353#discussion_r844861313

phaus avatar Apr 16 '23 13:04 phaus

https://github.com/appwrite/sdk-generator/blob/55f8d82d90287a4f69b26297d559822e1d9475c7/templates/go/client.go.twig#L311 There doesn't seem to be a result field in current version of appwrite. Was it present in older versions of appwrite?

Suven-p avatar Apr 16 '23 16:04 Suven-p

@phaus https://github.com/appwrite/sdk-generator/pull/647 is getting quite large. Should I split the PR?

Suven-p avatar Apr 20 '23 11:04 Suven-p

My PR was also quite big: https://github.com/appwrite/sdk-generator/pull/353 Maybe @lohanidamodar or @abnegate can have a look and provide some feedback :-)

phaus avatar Apr 20 '23 11:04 phaus

This doesn't work with appwrite 1.3.1 because optional parameters do not accept falsy values. For example: when creating string attribute on a collection without a default value, older version allowed using empty string as default value. But in appwrite 1.3.1 if the parameter is sent it should be a valid value ie not an empty string. I am not sure how to filter out those values.

Suven-p avatar Apr 21 '23 18:04 Suven-p

@phaus When creating an integer attribute on a collection, the user has to pass in argument for default since Go does not have optional arguments. For string arguments it can be ignored if it is an empty string. However for integer and float the user might have meant no default argument or default argument of 0. How should this be handled?

Suven-p avatar Apr 21 '23 19:04 Suven-p

@Suven-p If I found some time again, I can have a look. Do you have any specific code where is issue is located?

phaus avatar Apr 21 '23 20:04 phaus

I dont have an example in Go but here's the equivalent method in python

def create_float_attribute(self, database_id, collection_id, key, required, min = None, max = None, default = None, array = None):

Here the default value for min, max and default parameters is None which means user doesn't want to specify them I don't see any way to implement this in Go.

Suven-p avatar Apr 21 '23 20:04 Suven-p

@Suven-p We can use optional variadics like "..." or use a pointer I guess

SoulPancake avatar Apr 23 '23 08:04 SoulPancake

Variadics would need to be of type interface{} and that would forego typechecking. Pointers don't allow literals like 23. I was thinking of doing functional options. It would be a struct for each endpoint but that's probably the best way.

Suven-p avatar Apr 23 '23 08:04 Suven-p

Hey @eldadfux , plz assign it to me i would really love to work on this 😄

AryanParashar24 avatar Oct 04 '23 12:10 AryanParashar24

Me and @mirkobrombin are happy to take care of the Go SDK generator ✌🏼

pietrodicaprio avatar Oct 06 '23 17:10 pietrodicaprio

Any update on this?

gaby avatar Mar 10 '24 03:03 gaby