You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’ve recently started developing with Goa, and I’m still learning about all its functionalities. I’m currently trying to convert a user-defined type into a struct in a library. For this specific example, I want to handle a currency code and amount, using the currency library for internal representation.
Here’s the code I’ve implemented so far. However, I noticed that the library internally uses private fields and only exposes constructors to create the Amount object:
var CurrencyType = Type("Currency", func() {
Description("Currency amount")
TypeName("CurrencyType")
ConvertTo(domain.Currency{})
Attribute("currencyCode", String, "Currency code in ISO 4217", func() {
Pattern(`^[A-Z]{3}$`)
Example("USD")
MaxLength(3)
MinLength(3)
})
Attribute("number", String, "Amount of currency", func() {
Pattern(`^\d+(\.\d{1,6})?$`)
Example("1234.56")
})
Required("currencyCode", "number")
})
The constructor provided by the library, which I find useful, is:
It’s straightforward to implement the conversion for the CurrencyType field in my own packages. However, when CurrencyType is part of a more complex user-defined type, I’m unsure how to specify the constructor instead of directly mapping fields during the conversion.
My question is: Is there a way to parameterize the constructor to map a specific field when using ConvertTo?
The text was updated successfully, but these errors were encountered:
Hello! There isn't currently a way to specify a constructor. A typical pattern used in similar situations is to add helper functions to the service methods that translate the data structures between the endpoint layer and the business logic / data layer.
Thanks a lot for the answer. I was looking for a workaround and found an alternative solution. I defined the field using the struct:field:type meta tag, and it worked:
However, I lost the ability to define the internal structure, even though the type has marshaling and unmarshaling methods. I tried using a custom-defined type, but when I do that, the meta tag behavior is ignored. I’m not sure if it’s intended to work this way or not.
It would be great if we could use custom definitions that are mapped to a custom type.
Hi!
I’ve recently started developing with Goa, and I’m still learning about all its functionalities. I’m currently trying to convert a user-defined type into a struct in a library. For this specific example, I want to handle a currency code and amount, using the currency library for internal representation.
Here’s the code I’ve implemented so far. However, I noticed that the library internally uses private fields and only exposes constructors to create the
Amount
object:The constructor provided by the library, which I find useful, is:
func NewAmount(n, currencyCode string) (Amount, error)
It’s straightforward to implement the conversion for the
CurrencyType
field in my own packages. However, whenCurrencyType
is part of a more complex user-defined type, I’m unsure how to specify the constructor instead of directly mapping fields during the conversion.My question is: Is there a way to parameterize the constructor to map a specific field when using
ConvertTo
?The text was updated successfully, but these errors were encountered: