Coder Social home page Coder Social logo

Comments (7)

benjaminjkraft avatar benjaminjkraft commented on September 27, 2024

Can you explain more about what isn't working and why json:"-" is causing problems? Are you seeing actual serialization errors? Anytime genqlient adds that it should also add appropriate MarshalJSON/UnmarshalJSON (in this case to Webhook) to ensure everything serializes correctly.

from genqlient.

aawnu avatar aawnu commented on September 27, 2024

When I look at the code there is a marshal and unmashal, but when I use the Webhook inside Temporal (passes json between activities) then it does not encode the json.

// Webhook includes the requested fields of the GraphQL type Webhook.
type Webhook struct {
	webhookFields `json:"-"`
}

// GetId returns Webhook.Id, and is useful for accessing the field via an interface.
func (v *Webhook) GetId() string { return v.webhookFields.Id }

// GetName returns Webhook.Name, and is useful for accessing the field via an interface.
func (v *Webhook) GetName() string { return v.webhookFields.Name }

// GetSource returns Webhook.Source, and is useful for accessing the field via an interface.
func (v *Webhook) GetSource() string { return v.webhookFields.Source }

// GetEnabled returns Webhook.Enabled, and is useful for accessing the field via an interface.
func (v *Webhook) GetEnabled() bool { return v.webhookFields.Enabled }

// GetCreatedAt returns Webhook.CreatedAt, and is useful for accessing the field via an interface.
func (v *Webhook) GetCreatedAt() time.Time { return v.webhookFields.CreatedAt }

func (v *Webhook) UnmarshalJSON(b []byte) error {

	if string(b) == "null" {
		return nil
	}

	var firstPass struct {
		*Webhook
		graphql.NoUnmarshalJSON
	}
	firstPass.Webhook = v

	err := json.Unmarshal(b, &firstPass)
	if err != nil {
		return err
	}

	err = json.Unmarshal(
		b, &v.webhookFields)
	if err != nil {
		return err
	}
	return nil
}

type __premarshalWebhook struct {
	Id string `json:"id"`

	Name string `json:"name"`

	Source string `json:"source"`

	Enabled bool `json:"enabled"`

	CreatedAt time.Time `json:"createdAt"`
}

func (v *Webhook) MarshalJSON() ([]byte, error) {
	premarshaled, err := v.__premarshalJSON()
	if err != nil {
		return nil, err
	}
	return json.Marshal(premarshaled)
}

func (v *Webhook) __premarshalJSON() (*__premarshalWebhook, error) {
	var retval __premarshalWebhook

	retval.Id = v.webhookFields.Id
	retval.Name = v.webhookFields.Name
	retval.Source = v.webhookFields.Source
	retval.Enabled = v.webhookFields.Enabled
	retval.CreatedAt = v.webhookFields.CreatedAt
	return &retval, nil
}

If I write a hack for it, it does not trigger

func main() {
	webhook := graphql.Webhook{}
	webhook.Name = "Cookie"
	webhook.CreatedAt = time.Now()

	_, j := json.Marshal(webhook)
	log.Printf("json=%s", j)
}
> go run main.go
2024/02/21 14:23:19 json=%!s(<nil>)

from genqlient.

aawnu avatar aawnu commented on September 27, 2024

I can not explain why it works, but if I remove the json:"-" then it seems to serialize fine within Temporal. I did however make my own serialization class to ensure it would not end up not serializing on future changes (regenerations).

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on September 27, 2024

Ah, you probably want to pass a pointer (&webhook) so the receiver works. That tends to be the convention for anything JSON, although maybe we could make it a value receiver so it doesn't matter? That might require some research, not sure if there are perf or complexity issues.

from genqlient.

aawnu avatar aawnu commented on September 27, 2024

I do unfortunately not determine how temporal passes data between workers/activities, so right now I use the middleware struct (basically a copy of the root/internal struct). It would be interesting if this somehow could be worked out :D

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on September 27, 2024

Can you not pass the pointer in? Does Temporal dereference the pointer?

In any case, I think you'll need to give a more complete example of what your code is doing with Temporal for us to help. In the code you listed, the fix is to pass a pointer. If that's not what your code is really doing, many other problems could be at play, whether with genqlient or Temporal.

from genqlient.

aawnu avatar aawnu commented on September 27, 2024

I'm sorry for never getting back to you, a few things have changed and I will soon no longer have access to this code I am working on. Due to its work relation I am unable to make you a replicate of my issue. If I encounter it in my own project then I will return with example repo if possible. I will close my issue due to this change of accessibility.

from genqlient.

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.