Skip to content

add custom json package for imports instead encoding/json support #261

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alimy
Copy link

@alimy alimy commented Apr 20, 2025

  • add custom json package for imports instead encoding/json support.

Usage:

//go:generate go-enum --marshal --sqlnullint --jsonpkg github.com/alimy/typst/json

/*
RunStatus
ENUM(

	None, // 未知
	Idle, // 空闲
	Pending, // 待命状态
	Processing, // 正在处理

)
*/
type RunStatus int

Will generate code like below:

package enum

import (
	"database/sql/driver"
	"errors"
	"fmt"
	"strconv"

	json "github.com/alimy/typst/json"
)

// other code
...

// MarshalJSON correctly serializes a NullRunStatus to JSON.
func (n NullRunStatus) MarshalJSON() ([]byte, error) {
	const nullStr = "null"
	if n.Valid {
		return json.Marshal(n.RunStatus)
	}
	return []byte(nullStr), nil
}

// UnmarshalJSON correctly deserializes a NullRunStatus from JSON.
func (n *NullRunStatus) UnmarshalJSON(b []byte) error {
	n.Set = true
	var x interface{}
	err := json.Unmarshal(b, &x)
	if err != nil {
		return err
	}
	err = n.Scan(x)
	return err
}

@alimy alimy requested a review from abice as a code owner April 20, 2025 09:53
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

Successfully merging this pull request may close these issues.

1 participant