MCPcopy
hub / github.com/google/mangle / JSONtoStruct

Function JSONtoStruct

json2struct/json2struct.go:26–44  ·  view source on GitHub ↗

JSONtoStruct converts a JSON blob to a Mangle struct.

(jsonBlob []byte)

Source from the content-addressed store, hash-verified

24
25// JSONtoStruct converts a JSON blob to a Mangle struct.
26func JSONtoStruct(jsonBlob []byte) (ast.Constant, error) {
27 var m map[string]any
28 if err := json.Unmarshal(jsonBlob, &m); err != nil {
29 return ast.Constant{}, err
30 }
31 structEntries := map[*ast.Constant]*ast.Constant{}
32 for k, v := range m {
33 fieldName, err := ast.Name("/" + k)
34 if err != nil {
35 return ast.Constant{}, err
36 }
37 value, err := ConvertValue(v)
38 if err != nil {
39 return ast.Constant{}, err
40 }
41 structEntries[&fieldName] = &value
42 }
43 return *ast.Struct(structEntries), nil
44}
45
46// ConvertValue converts an encoding/json value into a Mangle value.
47func ConvertValue(value any) (ast.Constant, error) {

Callers 1

TestJSONtoStructFunction · 0.85

Calls 3

NameFunction · 0.92
StructFunction · 0.92
ConvertValueFunction · 0.85

Tested by 1

TestJSONtoStructFunction · 0.68