Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Meta to Specify time.Time Type Causes Code Generation Error #3595

Open
Kahoulam opened this issue Sep 19, 2024 · 4 comments
Open

Using Meta to Specify time.Time Type Causes Code Generation Error #3595

Kahoulam opened this issue Sep 19, 2024 · 4 comments

Comments

@Kahoulam
Copy link

Description

In my design file, I use the following code to specify a date-time format field as time.Time type:

Attribute(name, String, func() {
    Meta("struct:field:type", "time.Time", "time") // Specify struct field type metadata
    Description(desc)                              // Set description of the attribute
    Format(FormatDateTime)                         // Set format for date and time
    Example("2024-01-01T11:53:54Z")                // Provide example value
})

The reason for writing it this way is to avoid repeatedly converting between time.Time and string.

However, the generated code produces the following error:

cannot use *body.Timestamp (variable of type time.Time) as string value in argument to goa.ValidateFormat

Example

Below is the complete design file example:

package design

import (
    . "goa.design/goa/v3/dsl"
)

var _ = API("example", func() {
    Title("Example API")
    Server("example", func() {
        Host("localhost", func() {
            URI("http://localhost:8080")
        })
    })
})

var ExampleType = Type("ExampleType", func() {
    Attribute("timestamp", String, func() {
        Meta("struct:field:type", "time.Time", "time") // Specify struct field type metadata
        Description("Timestamp in RFC3339 format")
        Format(FormatDateTime)
        Example("2024-01-01T11:53:54Z")
    })
    Required("timestamp")
})

var _ = Service("example", func() {
    Method("show", func() {
        Payload(ExampleType)
        Result(ExampleType)
        HTTP(func() {
            GET("/example")
            Response(StatusOK)
        })
    })
})

Expected Solution

I hope to be able to specify a date-time format field as time.Time type in the design file using Meta, and have the generated code work without the above error.

Additional Information

goa version: v3.16.2
Golang version: go1.22.6
Operating System: linux/arm64

I would appreciate any solutions or guidance. Thank you!

@Kahoulam Kahoulam changed the title Using Meta to Specify time.Time Type in goa.design Causes Code Generation Error Using Meta to Specify time.Time Type Causes Code Generation Error Sep 19, 2024
@raphael
Copy link
Member

raphael commented Sep 20, 2024

Hello, the type specified with struct:field:type must be structurally compatible with the type defined in the design, here time.Time isn't compatible with String.

@Kahoulam
Copy link
Author

Kahoulam commented Sep 24, 2024

Hello, the type specified with struct:field:type must be structurally compatible with the type defined in the design, here time.Time isn't compatible with String.

@raphael Thank you for your reply.

According to the documentation, the struct:field:type meta tag allows you to override the default Go struct field type specified in the design.

The error occurs because the validation code generated by Format(FormatDateTime) is not compatible.

My goal is to use time.Time directly in Go and to include format: date-time in the API specification.

@raphael
Copy link
Member

raphael commented Sep 24, 2024

The goal makes sense, it won't work out of the box though as Goa can't possibly know how to convert any type to any other type.

@tchssk
Copy link
Member

tchssk commented Sep 25, 2024

I'm considering this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants