package server import ( "context" "fmt" "strconv" "git.xiaotuo.co/pkg/go-plugins/grpc/metax" ) func GetBizId(ctx context.Context) (int64, error) { bizId := getContextValue(ctx, "biz_id") if bizId == "" { return 0, fmt.Errorf("biz_id_is_empty") } return strconv.ParseInt(bizId, 10, 64) } func NewContext(ctx context.Context) (context.Context, error) { ctx = metax.TransparentContext(ctx) reqId, exists, err := metax.GetReqId(ctx) if err != nil { return ctx, fmt.Errorf("req_id_parse_fail") } if !exists { return ctx, fmt.Errorf("req_id_not_found") } ctx = context.WithValue(ctx, "req_id", reqId) bizId, exists, err := metax.GetBizId(ctx) if err != nil { return ctx, fmt.Errorf("biz_id_parse_fail") } if !exists { return ctx, fmt.Errorf("biz_id_not_found") } ctx = context.WithValue(ctx, "biz_id", bizId) return ctx, nil } func getContextValue(ctx context.Context, keyword string) string { if ctx.Value(keyword) != nil { if v, ok := ctx.Value(keyword).(string); ok { return v } } return "" }