Skip to content

Conversation

@alimy
Copy link
Contributor

@alimy alimy commented Jun 17, 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
}

@abice abice merged commit 0979fc7 into abice:master Jun 17, 2025
2 checks passed
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.

2 participants