Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install dependencies
run: go get .
- name: Test with the Go CLI
run: go test -v ./...
run: GOEXPERIMENT=jsonv2,greenteagc go test -v ./...

build:
needs: [test]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY go.mod go.sum ./
RUN if [ ! -f vendor/modules.txt ]; then go mod download; fi

COPY . .
RUN CGO_ENABLED=0 go build -tags go_json -o /go/bin/golbat
RUN CGO_ENABLED=0 GOEXPERIMENT=jsonv2,greenteagc go build -o /go/bin/golbat
RUN mkdir /empty-dir

# Now copy it into our base image.
Expand Down
19 changes: 8 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
FLAGS = go_json

# Alternative to switch in the sonic json library
golbat: FORCE
GOEXPERIMENT=jsonv2,greenteagc go build golbat

#ARCH=$(shell arch)
#ifeq ($(ARCH),x86_64)
#FLAGS := sonic
#else
#FLAGS := go_json
#endif
proto: FORCE
python3 scripts/add_lazy_proto.py
protoc --go_out=pogo --go_opt=paths=source_relative --go_opt=default_api_level=API_OPAQUE --go_opt=Mvbase.proto=golbat/pogo vbase.proto

golbat: FORCE
go build -tags $(FLAGS) golbat
proto-lazy-all: FORCE
python3 scripts/add_lazy_proto.py --all
protoc --go_out=pogo --go_opt=paths=source_relative --go_opt=default_api_level=API_OPAQUE --go_opt=Mvbase.proto=golbat/pogo vbase.proto

FORCE: ;
61 changes: 61 additions & 0 deletions codec/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package codec

import (
"io"

"encoding/json/jsontext"
"encoding/json/v2"

gojson "github.com/goccy/go-json"
)

// UseGoJSON controls whether to use go-json for marshal/unmarshal operations.
// Default is false (use jsonv2).
var UseGoJSON bool

// JSONMarshal encodes v into JSON.
// Uses go-json if UseGoJSON is true, otherwise uses jsonv2.
func JSONMarshal(v any) ([]byte, error) {
if UseGoJSON {
return gojson.Marshal(v)
}
return json.Marshal(v)
}

// JSONMarshalIndent encodes v into indented JSON.
// Uses go-json if UseGoJSON is true, otherwise uses jsonv2.
func JSONMarshalIndent(v any, prefix, indent string) ([]byte, error) {
if UseGoJSON {
return gojson.MarshalIndent(v, prefix, indent)
}
return json.Marshal(v, jsontext.WithIndent(indent))
}

// JSONUnmarshal decodes JSON data into v.
// Uses go-json if UseGoJSON is true, otherwise uses jsonv2.
func JSONUnmarshal(data []byte, v any) error {
if UseGoJSON {
return gojson.Unmarshal(data, v)
}
return json.Unmarshal(data, v)
}

// JSONUnmarshalRead decodes JSON from reader into v.
// Uses go-json if UseGoJSON is true, otherwise uses jsonv2.
func JSONUnmarshalRead(r io.Reader, v any) error {
if UseGoJSON {
return gojson.NewDecoder(r).Decode(v)
}
return json.UnmarshalRead(r, v)
}

// JSONMarshalWrite encodes v as JSON directly to an io.Writer.
// This is more efficient than JSONMarshal when writing to a stream
// as it avoids intermediate []byte allocation.
// Uses go-json if UseGoJSON is true, otherwise uses jsonv2.
func JSONMarshalWrite(w io.Writer, v any) error {
if UseGoJSON {
return gojson.NewEncoder(w).Encode(v)
}
return json.MarshalWrite(w, v)
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type tuning struct {
MaxPokemonResults int `koanf:"max_pokemon_results"`
MaxPokemonDistance float64 `koanf:"max_pokemon_distance"`
ProfileRoutes bool `koanf:"profile_routes"`
UseGoJSON bool `koanf:"use_go_json"`
}

type scanRule struct {
Expand Down
106 changes: 52 additions & 54 deletions decoder/api_pokemon_common.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
package decoder

import (
"github.com/UnownHash/gohbem"
"github.com/UnownHash/gohbem"
)

func buildApiPokemonResult(pokemon *Pokemon) ApiPokemonResult {
return ApiPokemonResult{
Id: pokemon.Id,
PokestopId: pokemon.PokestopId,
SpawnId: pokemon.SpawnId,
Lat: pokemon.Lat,
Lon: pokemon.Lon,
Weight: pokemon.Weight,
Size: pokemon.Size,
Height: pokemon.Height,
ExpireTimestamp: pokemon.ExpireTimestamp,
Updated: pokemon.Updated,
PokemonId: pokemon.PokemonId,
Move1: pokemon.Move1,
Move2: pokemon.Move2,
Gender: pokemon.Gender,
Cp: pokemon.Cp,
AtkIv: pokemon.AtkIv,
DefIv: pokemon.DefIv,
StaIv: pokemon.StaIv,
Iv: pokemon.Iv,
Form: pokemon.Form,
Level: pokemon.Level,
Weather: pokemon.Weather,
Costume: pokemon.Costume,
FirstSeenTimestamp: pokemon.FirstSeenTimestamp,
Changed: pokemon.Changed,
CellId: pokemon.CellId,
ExpireTimestampVerified: pokemon.ExpireTimestampVerified,
DisplayPokemonId: pokemon.DisplayPokemonId,
IsDitto: pokemon.IsDitto,
SeenType: pokemon.SeenType,
Shiny: pokemon.Shiny,
Username: pokemon.Username,
Pvp: func() map[string][]gohbem.PokemonEntry {
if ohbem != nil {
pvp, err := ohbem.QueryPvPRank(int(pokemon.PokemonId),
int(pokemon.Form.ValueOrZero()),
int(pokemon.Costume.ValueOrZero()),
int(pokemon.Gender.ValueOrZero()),
int(pokemon.AtkIv.ValueOrZero()),
int(pokemon.DefIv.ValueOrZero()),
int(pokemon.StaIv.ValueOrZero()),
float64(pokemon.Level.ValueOrZero()))
if err != nil {
return nil
}
return pvp
}
return nil
}(),
}
return ApiPokemonResult{
Id: pokemon.Id,
PokestopId: pokemon.PokestopId,
SpawnId: pokemon.SpawnId,
Lat: pokemon.Lat,
Lon: pokemon.Lon,
Weight: pokemon.Weight,
Size: pokemon.Size,
Height: pokemon.Height,
ExpireTimestamp: pokemon.ExpireTimestamp,
Updated: pokemon.Updated,
PokemonId: pokemon.PokemonId,
Move1: pokemon.Move1,
Move2: pokemon.Move2,
Gender: pokemon.Gender,
Cp: pokemon.Cp,
AtkIv: pokemon.AtkIv,
DefIv: pokemon.DefIv,
StaIv: pokemon.StaIv,
Iv: pokemon.Iv,
Form: pokemon.Form,
Level: pokemon.Level,
Weather: pokemon.Weather,
Costume: pokemon.Costume,
FirstSeenTimestamp: pokemon.FirstSeenTimestamp,
Changed: pokemon.Changed,
CellId: pokemon.CellId,
ExpireTimestampVerified: pokemon.ExpireTimestampVerified,
DisplayPokemonId: pokemon.DisplayPokemonId,
IsDitto: pokemon.IsDitto,
SeenType: pokemon.SeenType,
Shiny: pokemon.Shiny,
Username: pokemon.Username,
Pvp: func() map[string][]gohbem.PokemonEntry {
if ohbem != nil {
pvp, err := ohbem.QueryPvPRank(int(pokemon.PokemonId),
int(pokemon.Form.ValueOrZero()),
int(pokemon.Costume.ValueOrZero()),
int(pokemon.Gender.ValueOrZero()),
int(pokemon.AtkIv.ValueOrZero()),
int(pokemon.DefIv.ValueOrZero()),
int(pokemon.StaIv.ValueOrZero()),
float64(pokemon.Level.ValueOrZero()))
if err != nil {
return nil
}
return pvp
}
return nil
}(),
}
}


Loading