github.com/minio/simdjson-go@v0.4.6-0.20231116094823-04d21cddf993/parsed_object_test.go (about)

     1  package simdjson
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"testing"
     7  )
     8  
     9  func TestObject_FindPath(t *testing.T) {
    10  	if !SupportedCPU() {
    11  		t.SkipNow()
    12  	}
    13  	tests := []struct {
    14  		name     string
    15  		path     []string
    16  		wantName string
    17  		wantType Type
    18  		wantVal  string
    19  		wantErr  bool
    20  	}{
    21  		{
    22  			name:     "top",
    23  			path:     []string{"Alt"},
    24  			wantName: "Alt",
    25  			wantType: TypeString,
    26  			wantVal:  `"Image of city"`,
    27  		},
    28  		{
    29  			name:     "nested-1",
    30  			path:     []string{"Image", "Animated"},
    31  			wantName: "Animated",
    32  			wantType: TypeBool,
    33  			wantVal:  "false",
    34  		},
    35  		{
    36  			name:     "nested-2",
    37  			path:     []string{"Image", "Thumbnail", "Url"},
    38  			wantName: "Url",
    39  			wantType: TypeString,
    40  			wantVal:  `"http://www.example.com/image/481989943"`,
    41  		},
    42  		{
    43  			name:     "int",
    44  			path:     []string{"Image", "Height"},
    45  			wantName: "Height",
    46  			wantType: TypeInt,
    47  			wantVal:  `600`,
    48  		},
    49  		{
    50  			name:     "obj",
    51  			path:     []string{"Image", "Thumbnail"},
    52  			wantName: "Thumbnail",
    53  			wantType: TypeObject,
    54  			wantVal:  `{"Height":125,"Url":"http://www.example.com/image/481989943","Width":100}`,
    55  		},
    56  		{
    57  			name:     "array",
    58  			path:     []string{"Image", "IDs"},
    59  			wantName: "IDs",
    60  			wantType: TypeArray,
    61  			wantVal:  `[116,943,234,38793]`,
    62  		},
    63  		{
    64  			name:    "404",
    65  			path:    []string{"Image", "NonEx"},
    66  			wantErr: true,
    67  		},
    68  	}
    69  	input := `{
    70      "Image":
    71      {
    72          "Animated": false,
    73          "Height": 600,
    74          "IDs":
    75          [
    76              116,
    77              943,
    78              234,
    79              38793
    80          ],
    81          "Thumbnail":
    82          {
    83              "Height": 125,
    84              "Url": "http://www.example.com/image/481989943",
    85              "Width": 100
    86          },
    87          "Title": "View from 15th Floor",
    88          "Width": 800
    89      },
    90  	"Alt": "Image of city" 
    91  }`
    92  	for _, tt := range tests {
    93  		pj, err := Parse([]byte(input), nil)
    94  		if err != nil {
    95  			t.Fatal(err)
    96  		}
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			i := pj.Iter()
    99  			i.AdvanceInto()
   100  			_, root, err := i.Root(nil)
   101  			if err != nil {
   102  				t.Fatal(err)
   103  			}
   104  			obj, err := root.Object(nil)
   105  			if err != nil {
   106  				t.Fatal(err)
   107  			}
   108  
   109  			elem, err := obj.FindPath(nil, tt.path...)
   110  			if err != nil && !tt.wantErr {
   111  				t.Fatal(err)
   112  			}
   113  			if tt.wantErr {
   114  				return
   115  			}
   116  			if elem.Type != tt.wantType {
   117  				t.Errorf("Want type %v, got %v", tt.wantType, elem.Type)
   118  			}
   119  			if elem.Name != tt.wantName {
   120  				t.Errorf("Want name %v, got %v", tt.wantName, elem.Name)
   121  			}
   122  			ser, err := elem.Iter.MarshalJSON()
   123  			if err != nil {
   124  				t.Fatal(err)
   125  			}
   126  			if string(ser) != tt.wantVal {
   127  				t.Errorf("want '%s', got '%s'", tt.wantVal, string(ser))
   128  			}
   129  		})
   130  	}
   131  }
   132  
   133  func ExampleObject_FindPath() {
   134  	if !SupportedCPU() {
   135  		// Fake it
   136  		fmt.Println("string\nhttp://www.example.com/image/481989943 <nil>")
   137  		return
   138  	}
   139  	input := `{
   140      "Image":
   141      {
   142          "Animated": false,
   143          "Height": 600,
   144          "IDs":
   145          [
   146              116,
   147              943,
   148              234,
   149              38793
   150          ],
   151          "Thumbnail":
   152          {
   153              "Height": 125,
   154              "Url": "http://www.example.com/image/481989943",
   155              "Width": 100
   156          },
   157          "Title": "View from 15th Floor",
   158          "Width": 800
   159      },
   160  	"Alt": "Image of city" 
   161  }`
   162  	pj, err := Parse([]byte(input), nil)
   163  	if err != nil {
   164  		log.Fatal(err)
   165  	}
   166  	i := pj.Iter()
   167  	i.AdvanceInto()
   168  
   169  	// Grab root
   170  	_, root, err := i.Root(nil)
   171  	if err != nil {
   172  		log.Fatal(err)
   173  	}
   174  	// Grab top object
   175  	obj, err := root.Object(nil)
   176  	if err != nil {
   177  		log.Fatal(err)
   178  	}
   179  
   180  	// Find element in path.
   181  	elem, err := obj.FindPath(nil, "Image", "Thumbnail", "Url")
   182  	if err != nil {
   183  		log.Fatal(err)
   184  	}
   185  
   186  	// Print result:
   187  	fmt.Println(elem.Type)
   188  	fmt.Println(elem.Iter.String())
   189  
   190  	// Output:
   191  	// string
   192  	// http://www.example.com/image/481989943 <nil>
   193  }
   194  
   195  func ExampleArray() {
   196  	if !SupportedCPU() {
   197  		// Fake it
   198  		fmt.Println("Found array\nType: int value: 116\nType: int value: 943\nType: int value: 234\nType: int value: 38793")
   199  		return
   200  	}
   201  	input := `{
   202      "Image":
   203      {
   204          "Animated": false,
   205          "Height": 600,
   206          "IDs":
   207          [
   208              116,
   209              943,
   210              234,
   211              38793
   212          ],
   213          "Thumbnail":
   214          {
   215              "Height": 125,
   216              "Url": "http://www.example.com/image/481989943",
   217              "Width": 100
   218          },
   219          "Title": "View from 15th Floor",
   220          "Width": 800
   221      },
   222  	"Alt": "Image of city" 
   223  }`
   224  	pj, err := Parse([]byte(input), nil)
   225  	if err != nil {
   226  		log.Fatal(err)
   227  	}
   228  	i := pj.Iter()
   229  	i.AdvanceInto()
   230  
   231  	// Grab root
   232  	_, root, err := i.Root(nil)
   233  	if err != nil {
   234  		log.Fatal(err)
   235  	}
   236  	// Grab top object
   237  	obj, err := root.Object(nil)
   238  	if err != nil {
   239  		log.Fatal(err)
   240  	}
   241  
   242  	// Find element in path.
   243  	elem, err := obj.FindPath(nil, "Image", "IDs")
   244  	if err != nil {
   245  		log.Fatal(err)
   246  	}
   247  	fmt.Println("Found", elem.Type)
   248  	if elem.Type == TypeArray {
   249  		array, err := elem.Iter.Array(nil)
   250  		if err != nil {
   251  			log.Fatal(err)
   252  		}
   253  		array.ForEach(func(i Iter) {
   254  			asString, _ := i.StringCvt()
   255  			fmt.Println("Type:", i.Type(), "value:", asString)
   256  		})
   257  	}
   258  	//Output:
   259  	//Found array
   260  	//Type: int value: 116
   261  	//Type: int value: 943
   262  	//Type: int value: 234
   263  	//Type: int value: 38793
   264  }
   265  
   266  func ExampleArray_DeleteElems() {
   267  	if !SupportedCPU() {
   268  		// Fake it
   269  		fmt.Println("Found array\nModified: {\"Image\":{\"Animated\":false,\"Height\":600,\"IDs\":[943,38793]},\"Alt\":\"Image of city\"}")
   270  		return
   271  	}
   272  	input := `{
   273      "Image":
   274      {
   275          "Animated": false,
   276          "Height": 600,
   277          "IDs":
   278          [
   279              116,
   280              943,
   281              234,
   282              38793
   283          ]
   284      },
   285  	"Alt": "Image of city" 
   286  }`
   287  	pj, err := Parse([]byte(input), nil)
   288  	if err != nil {
   289  		log.Fatal(err)
   290  	}
   291  	i := pj.Iter()
   292  	i.AdvanceInto()
   293  
   294  	// Grab root
   295  	_, root, err := i.Root(nil)
   296  	if err != nil {
   297  		log.Fatal(err)
   298  	}
   299  	// Grab top object
   300  	obj, err := root.Object(nil)
   301  	if err != nil {
   302  		log.Fatal(err)
   303  	}
   304  
   305  	// Find element in path.
   306  	elem, err := obj.FindPath(nil, "Image", "IDs")
   307  	if err != nil {
   308  		log.Fatal(err)
   309  	}
   310  	fmt.Println("Found", elem.Type)
   311  	if elem.Type == TypeArray {
   312  		array, err := elem.Iter.Array(nil)
   313  		if err != nil {
   314  			log.Fatal(err)
   315  		}
   316  		// Delete all integer elements that are < 500
   317  		array.DeleteElems(func(i Iter) bool {
   318  			if id, err := i.Int(); err == nil {
   319  				return id < 500
   320  			}
   321  			return false
   322  		})
   323  	}
   324  	b, err := root.MarshalJSON()
   325  	if err != nil {
   326  		log.Fatal(err)
   327  	}
   328  	fmt.Println("Modified:", string(b))
   329  	//Output:
   330  	//Found array
   331  	//Modified: {"Image":{"Animated":false,"Height":600,"IDs":[943,38793]},"Alt":"Image of city"}
   332  }