-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps2.go
More file actions
69 lines (62 loc) 路 1.47 KB
/
Copy pathps2.go
File metadata and controls
69 lines (62 loc) 路 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ps2
import (
"bufio"
"bytes"
"encoding/json"
"io"
"strings"
"github.com/ddddddO/ps2/parser"
"github.com/goccy/go-yaml"
"github.com/pelletier/go-toml/v2"
)
func Run(input io.Reader, options ...Option) (string, error) {
cfg := NewConfig(options)
scanner := bufio.NewScanner(input)
phpSerializedString := ""
for scanner.Scan() {
phpSerializedString += scanner.Text()
}
if err := scanner.Err(); err != nil {
return "", err
}
parser := parser.New(phpSerializedString)
rootNode, err := parser.Parse()
if err != nil {
return "", err
}
buf := bytes.Buffer{}
encoder := json.NewEncoder(&buf)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
jsonRootNode := astNodeToJSONNode(rootNode)
// jsonRootNode.Children = nil
if err := encoder.Encode(jsonRootNode); err != nil {
return "", err
}
switch cfg.outputType {
case outputTypeJSON:
return strings.TrimSuffix(buf.String(), "\n"), nil
case outputTypeYAML:
y, err := yaml.JSONToYAML(buf.Bytes())
if err != nil {
return "", err
}
return strings.TrimSuffix(string(y), "\n"), nil
case outputTypeTOML:
d := json.NewDecoder(&buf)
buf2 := bytes.Buffer{}
e := toml.NewEncoder(&buf2)
d.UseNumber()
e.SetMarshalJsonNumbers(true)
var v interface{}
if err := d.Decode(&v); err != nil {
return "", err
}
if err := e.Encode(v); err != nil {
return "", err
}
return strings.TrimSuffix(buf2.String(), "\n"), nil
default:
return strings.TrimSuffix(buf.String(), "\n"), nil
}
}