Walk an object array (the only type of array supported)
(ctx context.Context, level int, a []*fastjson.Value, da interface{})
| 2018 | |
| 2019 | // Walk an object array (the only type of array supported) |
| 2020 | func (tmplContext *BulkTemplateContext) walkArray(ctx context.Context, level int, a []*fastjson.Value, da interface{}) (err error) { |
| 2021 | |
| 2022 | if a == nil { |
| 2023 | return |
| 2024 | } |
| 2025 | if len(a) == 0 { |
| 2026 | return |
| 2027 | } |
| 2028 | |
| 2029 | if debugEncoding { |
| 2030 | fmt.Printf("%swalkArray: %d elements\n", strings.Repeat(" ", level), len(a)) |
| 2031 | } |
| 2032 | |
| 2033 | // Special handling for single numeric template entry |
| 2034 | if len(a) == 1 && a[0].Type() == fastjson.TypeNumber { |
| 2035 | |
| 2036 | format := a[0].GetFloat64() |
| 2037 | tmplContext.binArrayTemplateValue = format |
| 2038 | tmplContext.binArrayCountOffset = uint32(len(tmplContext.Bin)) |
| 2039 | tmplContext.binArrayAppendMode = true |
| 2040 | |
| 2041 | // Check array length if da is an array |
| 2042 | switch dav := da.(type) { |
| 2043 | case []interface{}: |
| 2044 | if len(dav) > int(tmplContext.MaxArrayLength) { |
| 2045 | return fmt.Errorf("array length %d exceeds maximum of %d", len(dav), tmplContext.MaxArrayLength) |
| 2046 | } |
| 2047 | err = tmplContext.binAppendUint16(ctx, 0) |
| 2048 | for i := 0; i < len(dav); i++ { |
| 2049 | err = processArrayValue(ctx, tmplContext, level, i, a[0], dav) |
| 2050 | if err != nil { |
| 2051 | break |
| 2052 | } |
| 2053 | } |
| 2054 | case []string: |
| 2055 | if len(dav) > int(tmplContext.MaxArrayLength) { |
| 2056 | return fmt.Errorf("array length %d exceeds maximum of %d", len(dav), tmplContext.MaxArrayLength) |
| 2057 | } |
| 2058 | err = tmplContext.binAppendUint16(ctx, 0) |
| 2059 | for i := 0; i < len(dav); i++ { |
| 2060 | err = processArrayValue(ctx, tmplContext, level, i, a[0], dav) |
| 2061 | if err != nil { |
| 2062 | break |
| 2063 | } |
| 2064 | } |
| 2065 | case []float32: |
| 2066 | if len(dav) > int(tmplContext.MaxArrayLength) { |
| 2067 | return fmt.Errorf("array length %d exceeds maximum of %d", len(dav), tmplContext.MaxArrayLength) |
| 2068 | } |
| 2069 | err = tmplContext.binAppendUint16(ctx, 0) |
| 2070 | for i := 0; i < len(dav); i++ { |
| 2071 | err = processArrayValue(ctx, tmplContext, level, i, a[0], dav) |
| 2072 | if err != nil { |
| 2073 | break |
| 2074 | } |
| 2075 | } |
| 2076 | case []float64: |
| 2077 | if len(dav) > int(tmplContext.MaxArrayLength) { |
no test coverage detected