Skip to content

Commit

Permalink
Added makefile for parser
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed May 9, 2024
1 parent 8b95f4a commit c81ffdb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 47 deletions.
18 changes: 18 additions & 0 deletions parser/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
default: clean build copy-exec-scripts

build:
@echo "Building wasm..."
GOOS=js GOARCH=wasm go build -o parser.wasm -v .

copy-exec-scripts:
@echo "Copying exec scripts..."
cp $(shell go env GOROOT)/misc/wasm/wasm_exec.js .

clean:
@echo "Cleaning..."
rm -f parser.wasm
rm -f wasm_exec.js

test:
@echo "Testing..."
go test -v ./core/...
47 changes: 0 additions & 47 deletions parser/core/parse.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,9 @@
package core

import (
"bytes"
"encoding/json"
)

type ParsedData map[string]any

func (a *ParsedData) UnmarshalJSON(b []byte) error {
decoder := json.NewDecoder(bytes.NewReader(b))
decoder.UseNumber()

if *a == nil {
*a = ParsedData{}
}

obj := map[string]ParsedData{}
if err := json.Unmarshal(b, &obj); err == nil {
if obj == nil {
(*a)["type"] = "null"
} else {
(*a)["type"] = "object"
(*a)["properties"] = obj
}

return nil
}

arr := []ParsedData{}
if err := json.Unmarshal(b, &arr); err == nil {
(*a)["type"] = "array"
(*a)["items"] = arr
return nil
}

str := ""
if err := json.Unmarshal(b, &str); err == nil {
(*a)["type"] = "string"
(*a)["value"] = str
return nil
}

number := json.Number("")
if err := json.Unmarshal(b, &number); err == nil {
(*a)["type"] = "number"
(*a)["value"] = string(number)
return nil
}

return nil
}

func Parse(input string) (map[string]any, error) {
data := ParsedData{}
if err := json.Unmarshal([]byte(input), &data); err != nil {
Expand Down
52 changes: 52 additions & 0 deletions parser/core/parsed_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package core

import (
"bytes"
"encoding/json"
)

type ParsedData map[string]any

func (a *ParsedData) UnmarshalJSON(b []byte) error {
decoder := json.NewDecoder(bytes.NewReader(b))
decoder.UseNumber()

if *a == nil {
*a = ParsedData{}
}

obj := map[string]ParsedData{}
if err := json.Unmarshal(b, &obj); err == nil {
if obj == nil {
(*a)["type"] = "null"
} else {
(*a)["type"] = "object"
(*a)["properties"] = obj
}

return nil
}

arr := []ParsedData{}
if err := json.Unmarshal(b, &arr); err == nil {
(*a)["type"] = "array"
(*a)["items"] = arr
return nil
}

str := ""
if err := json.Unmarshal(b, &str); err == nil {
(*a)["type"] = "string"
(*a)["value"] = str
return nil
}

number := json.Number("")
if err := json.Unmarshal(b, &number); err == nil {
(*a)["type"] = "number"
(*a)["value"] = string(number)
return nil
}

return nil
}
Binary file removed parser/parser
Binary file not shown.
Binary file modified parser/parser.wasm
Binary file not shown.

0 comments on commit c81ffdb

Please sign in to comment.