Skip to content

Commit

Permalink
Add initial json.dumps support
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Phelps committed Jun 30, 2021
1 parent 32e3c0d commit 885572e
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4008,6 +4008,65 @@ func main() {
fmt.Println("The sum of", num1, "and", num2, "is", sum)
}
```
### jsondump
#### Python
```python
import json
import random


def main():
print(json.dumps(1))
print(json.dumps("hello"))
c = json.dumps({"hello": 1, "how": "are you"})
print(c + c)
print(json.dumps([1, 2, 3]))


if __name__ == '__main__':
main()
```
#### Go
```go
package main

import (
"encoding/json"
"fmt"
)

func main() {
fmt.Println(func() string {
b, err := json.Marshal(1)
if err != nil {
panic(err)
}
return string(b)
}())
fmt.Println(func() string {
b, err := json.Marshal("hello")
if err != nil {
panic(err)
}
return string(b)
}())
c := func() string {
b, err := json.Marshal(map[string]interface{}{"hello": 1, "how": "are you"})
if err != nil {
panic(err)
}
return string(b)
}()
fmt.Println(c + c)
fmt.Println(func() string {
b, err := json.Marshal([]int{1, 2, 3})
if err != nil {
panic(err)
}
return string(b)
}())
}
```
### algomajorityelement
#### Python
```python
Expand Down
38 changes: 38 additions & 0 deletions examples/jsondump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"encoding/json"
"fmt"
)

func main() {
fmt.Println(func() string {
b, err := json.Marshal(1)
if err != nil {
panic(err)
}
return string(b)
}())
fmt.Println(func() string {
b, err := json.Marshal("hello")
if err != nil {
panic(err)
}
return string(b)
}())
c := func() string {
b, err := json.Marshal(map[string]interface{}{"hello": 1, "how": "are you"})
if err != nil {
panic(err)
}
return string(b)
}()
fmt.Println(c + c)
fmt.Println(func() string {
b, err := json.Marshal([]int{1, 2, 3})
if err != nil {
panic(err)
}
return string(b)
}())
}
14 changes: 14 additions & 0 deletions examples/jsondump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json
import random


def main():
print(json.dumps(1))
print(json.dumps("hello"))
c = json.dumps({"hello": 1, "how": "are you"})
print(c + c)
print(json.dumps([1, 2, 3]))


if __name__ == '__main__':
main()
9 changes: 9 additions & 0 deletions pytago/go_ast/py_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,15 @@ def go_json_dumps(m): # pragma: no cover
return string(b)


obj = TypeVar("obj")
@Bindable.add(r"json\.loads", bind_type=BindType.FUNC_LIT, results=['obj'])
def go_json_loads(s: str) -> PytagoInterfaceType[obj]:
err = json.Unmarshal(bytes(s), '&' @ obj)
if err != nil:
panic(err)
return obj


# Logging methods

@Bindable.add(r"logging\.info", bind_type=BindType.EXPR)
Expand Down
12 changes: 12 additions & 0 deletions pytago/go_ast/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,17 @@ def visit_TypeSwitchStmt(self, node: TypeSwitchStmt):
return node


class RemoveConflictingImports(BaseTransformer):
REPEATABLE = False
def visit_GenDecl(self, node: GenDecl):
if len(node.Specs) == 1:
match node.Specs:
# If this is imported, encoding/json won't auto-import
case [ImportSpec(Path=BasicLit(Value='"json"'))]:
return
return node


ALL_TRANSFORMS = [
#### STAGE 0 ####
InsertUniqueInitializers,
Expand Down Expand Up @@ -1844,6 +1855,7 @@ def visit_TypeSwitchStmt(self, node: TypeSwitchStmt):
NodeTransformerWithInterfaceTypes,
TypeSwitchStatementsRedeclareWithType,
RemoveUnnecessaryFunctionLiterals,
RemoveConflictingImports,
RemoveGoCallReturns, # Needs to be below scoping functions or they'll just get added back
RemoveBadStmt, # Should be last as these are used for scoping
MergeAdjacentInits,
Expand Down
3 changes: 3 additions & 0 deletions pytago/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ def test_unpacking(self):
def test_cast_to_float(self):
self.assert_examples_match("cast_to_float")

def test_jsondump(self):
self.assert_examples_match("jsondump")

# Algorithms
def test_algomajorityelement(self):
self.assert_examples_match("algomajorityelement")
Expand Down

0 comments on commit 885572e

Please sign in to comment.