Code Monkey home page Code Monkey logo

Comments (4)

raphael avatar raphael commented on June 25, 2024

Can you provide a complete design that reproduces this problem? thank you!

from goa.

fg-j avatar fg-j commented on June 25, 2024

Surely.
Here's a full design.go

// This file contains descriptions of services and their methods.
// The methods should be implemented in, e.g. /example/api/service.go.
// Goa will roll up these descriptions and generate the corresponding endpoints.
package design

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

var _ = API("my-service", func() {
	Title("my-service")
	Description("Services related ")
	Server("my-service", func() {
		Host("localhost", func() {
			URI("http://localhost:8000")
		})
	})
})

var DeduplicateSpecification = Type("DeduplicateSpecification", func() {
	Attribute("matching_names", Boolean, func() {
		Description("de-duplicate when names match")
		Default(true)
	})
})

// Service describes a service
var _ = Service("my-service", func() {
	cors.Origin("*", func() {
		cors.Headers("*")
		cors.Methods("GET", "POST", "PUT", "DELETE", "PATCH")
		cors.Credentials()
	})

	Error("not_found")

	Method("create-dispatch", func() {
		Payload(func() {
			Attribute("name", String, "submission name")
			Attribute("deduplicate_by", DeduplicateSpecification, "how to deduplicate the submission")
		})
		Result(func() {
			Attribute("id", Int, "the submission id")
			Required("id")
		})
		HTTP(func() {
			POST("/submit")
			Response(StatusOK)
		})
	})
})

And here's the resulting generated gen/client/encode_decode.go with the problematic function:

func marshalMyserviceDeduplicateSpecificationToDeduplicateSpecificationRequestBody(v *myservice.DeduplicateSpecification) *DeduplicateSpecificationRequestBody {
	if v == nil {
		return nil
	}
	res := &DeduplicateSpecificationRequestBody{
		MatchingNames: v.MatchingNames,
	}
	{
		var zero bool
		if res.MatchingNames == zero {
			res.MatchingNames = true // this clobbers user-set false values
		}
	}

	return res
}

from goa.

raphael avatar raphael commented on June 25, 2024

Taking another look at this issue. I wonder what the behavior should be. The idea is that attributes that have a default value should generate fields that are not pointers, so in this case a field that is a bool and not a *bool. This would be difficult to change because it would break a lot of existing code. So I see two options:

  1. Make it an error to specify a default value for boolean fields. The goa gen tool would fail in this case with an error message indicating that boolean attributes can't have default values. The obvious drawback is that this behavior would preclude the generation of the transport level code that initializes the field to the default value if missing from the marshaled data.
  2. Change the code generation so that the marshaling side (client for payloads, server for results) does not check or set the default value - only the unmarshaling side would if the field is missing from the marshaled data.

Option 2 is probably preferable?

from goa.

github-actions avatar github-actions commented on June 25, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

from goa.

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.