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

move filter.js to go #162

Open
walkingeyerobot opened this issue Jul 22, 2020 · 0 comments
Open

move filter.js to go #162

walkingeyerobot opened this issue Jul 22, 2020 · 0 comments
Labels
server Something that Mitch needs to do

Comments

@walkingeyerobot
Copy link
Owner

This would avoid needing to have 2 running processes.

To handle JSON properly in Go, do something like this:

type OptionalInt []*int

func (i *OptionalInt) UnmarshalJSON(data []byte) error {
	if string(data) == "null" {
		*i = []*int{nil}
		return nil
	}

	var x int
	if err := json.Unmarshal(data, &x); err != nil {
		return err
	}
	*i = []*int{&x}
	return nil
}

func (i OptionalInt) MarshalJSON() ([]byte, error) {
	if len(i) == 0 {
		return []byte("null"), nil
	}
	return json.Marshal(i[0])
}

func (i OptionalInt) IsNull() bool {
	return len(i) == 1 && i[0] == nil
}

func (i OptionalInt) IsUndef() bool {
	return len(i) == 0
}

Given the above and some json unmarshalled into this struct:

type Foo struct {
	bar OptionalInt `json:",omitempty"`
}

To set to undefined:

foo.bar = []*int{}

To set to null:

foo.bar = []*int{nil}

To set to any other value:

x := 3
foo.bar = []*int{&x}

In addition to int, this will need to be done for string, bool, and float as well. Keep in mind there are no generics in go, so there will be a lot of repeated code for each type. It'll be ugly. Probably put it in something like jsontypes.go and filter.go, with both extra commented.

@walkingeyerobot walkingeyerobot added the server Something that Mitch needs to do label Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server Something that Mitch needs to do
Projects
None yet
Development

No branches or pull requests

1 participant