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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/**
* Copyright 2014 Paul Querna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package ffjsoninception
import (
"fmt"
"reflect"
"strings"
"github.com/pquerna/ffjson/shared"
)
var validValues []string = []string{
"FFTok_left_brace",
"FFTok_left_bracket",
"FFTok_integer",
"FFTok_double",
"FFTok_string",
"FFTok_bool",
"FFTok_null",
}
func CreateUnmarshalJSON(ic *Inception, si *StructInfo) error {
out := ""
ic.OutputImports[`fflib "github.com/pquerna/ffjson/fflib/v1"`] = true
if len(si.Fields) > 0 {
ic.OutputImports[`"bytes"`] = true
}
ic.OutputImports[`"fmt"`] = true
out += tplStr(decodeTpl["header"], header{
IC: ic,
SI: si,
})
out += tplStr(decodeTpl["ujFunc"], ujFunc{
SI: si,
IC: ic,
ValidValues: validValues,
ResetFields: ic.ResetFields,
})
ic.OutputFuncs = append(ic.OutputFuncs, out)
return nil
}
func handleField(ic *Inception, name string, typ reflect.Type, ptr bool, quoted bool) string {
return handleFieldAddr(ic, name, false, typ, ptr, quoted)
}
func handleFieldAddr(ic *Inception, name string, takeAddr bool, typ reflect.Type, ptr bool, quoted bool) string {
out := fmt.Sprintf("/* handler: %s type=%v kind=%v quoted=%t*/\n", name, typ, typ.Kind(), quoted)
umlx := typ.Implements(unmarshalFasterType) || typeInInception(ic, typ, shared.MustDecoder)
umlx = umlx || reflect.PtrTo(typ).Implements(unmarshalFasterType)
umlstd := typ.Implements(unmarshalerType) || reflect.PtrTo(typ).Implements(unmarshalerType)
out += tplStr(decodeTpl["handleUnmarshaler"], handleUnmarshaler{
IC: ic,
Name: name,
Typ: typ,
Ptr: reflect.Ptr,
TakeAddr: takeAddr || ptr,
UnmarshalJSONFFLexer: umlx,
Unmarshaler: umlstd,
})
if umlx || umlstd {
return out
}
// TODO(pquerna): generic handling of token type mismatching struct type
switch typ.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
allowed := buildTokens(quoted, "FFTok_string", "FFTok_integer", "FFTok_null")
out += getAllowTokens(typ.Name(), allowed...)
out += getNumberHandler(ic, name, takeAddr || ptr, typ, "ParseInt")
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
allowed := buildTokens(quoted, "FFTok_string", "FFTok_integer", "FFTok_null")
out += getAllowTokens(typ.Name(), allowed...)
out += getNumberHandler(ic, name, takeAddr || ptr, typ, "ParseUint")
case reflect.Float32,
reflect.Float64:
allowed := buildTokens(quoted, "FFTok_string", "FFTok_double", "FFTok_integer", "FFTok_null")
out += getAllowTokens(typ.Name(), allowed...)
out += getNumberHandler(ic, name, takeAddr || ptr, typ, "ParseFloat")
case reflect.Bool:
ic.OutputImports[`"bytes"`] = true
ic.OutputImports[`"errors"`] = true
allowed := buildTokens(quoted, "FFTok_string", "FFTok_bool", "FFTok_null")
out += getAllowTokens(typ.Name(), allowed...)
out += tplStr(decodeTpl["handleBool"], handleBool{
Name: name,
Typ: typ,
TakeAddr: takeAddr || ptr,
})
case reflect.Ptr:
out += tplStr(decodeTpl["handlePtr"], handlePtr{
IC: ic,
Name: name,
Typ: typ,
Quoted: quoted,
})
case reflect.Array,
reflect.Slice:
out += getArrayHandler(ic, name, typ, ptr)
case reflect.String:
// Is it a json.Number?
if typ.PkgPath() == "encoding/json" && typ.Name() == "Number" {
// Fall back to json package to rely on the valid number check.
// See: https://github.com/golang/go/blob/f05c3aa24d815cd3869153750c9875e35fc48a6e/src/encoding/json/decode.go#L897
ic.OutputImports[`"encoding/json"`] = true
out += tplStr(decodeTpl["handleFallback"], handleFallback{
Name: name,
Typ: typ,
Kind: typ.Kind(),
})
} else {
out += tplStr(decodeTpl["handleString"], handleString{
IC: ic,
Name: name,
Typ: typ,
TakeAddr: takeAddr || ptr,
Quoted: quoted,
})
}
case reflect.Interface:
ic.OutputImports[`"encoding/json"`] = true
out += tplStr(decodeTpl["handleFallback"], handleFallback{
Name: name,
Typ: typ,
Kind: typ.Kind(),
})
case reflect.Map:
out += tplStr(decodeTpl["handleObject"], handleObject{
IC: ic,
Name: name,
Typ: typ,
Ptr: reflect.Ptr,
TakeAddr: takeAddr || ptr,
})
default:
ic.OutputImports[`"encoding/json"`] = true
out += tplStr(decodeTpl["handleFallback"], handleFallback{
Name: name,
Typ: typ,
Kind: typ.Kind(),
})
}
return out
}
func getArrayHandler(ic *Inception, name string, typ reflect.Type, ptr bool) string {
if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
ic.OutputImports[`"encoding/base64"`] = true
useReflectToSet := false
if typ.Elem().Name() != "byte" {
ic.OutputImports[`"reflect"`] = true
useReflectToSet = true
}
return tplStr(decodeTpl["handleByteSlice"], handleArray{
IC: ic,
Name: name,
Typ: typ,
Ptr: reflect.Ptr,
UseReflectToSet: useReflectToSet,
})
}
if typ.Elem().Kind() == reflect.Struct && typ.Elem().Name() != "" {
goto sliceOrArray
}
if (typ.Elem().Kind() == reflect.Struct || typ.Elem().Kind() == reflect.Map) ||
typ.Elem().Kind() == reflect.Array || typ.Elem().Kind() == reflect.Slice &&
typ.Elem().Name() == "" {
ic.OutputImports[`"encoding/json"`] = true
return tplStr(decodeTpl["handleFallback"], handleFallback{
Name: name,
Typ: typ,
Kind: typ.Kind(),
})
}
sliceOrArray:
if typ.Kind() == reflect.Array {
return tplStr(decodeTpl["handleArray"], handleArray{
IC: ic,
Name: name,
Typ: typ,
IsPtr: ptr,
Ptr: reflect.Ptr,
})
}
return tplStr(decodeTpl["handleSlice"], handleArray{
IC: ic,
Name: name,
Typ: typ,
IsPtr: ptr,
Ptr: reflect.Ptr,
})
}
func getAllowTokens(name string, tokens ...string) string {
return tplStr(decodeTpl["allowTokens"], allowTokens{
Name: name,
Tokens: tokens,
})
}
func getNumberHandler(ic *Inception, name string, takeAddr bool, typ reflect.Type, parsefunc string) string {
return tplStr(decodeTpl["handlerNumeric"], handlerNumeric{
IC: ic,
Name: name,
ParseFunc: parsefunc,
TakeAddr: takeAddr,
Typ: typ,
})
}
func getNumberSize(typ reflect.Type) string {
return fmt.Sprintf("%d", typ.Bits())
}
func getType(ic *Inception, name string, typ reflect.Type) string {
s := typ.Name()
if typ.PkgPath() != "" && typ.PkgPath() != ic.PackagePath {
path := removeVendor(typ.PkgPath())
ic.OutputImports[`"`+path+`"`] = true
s = typ.String()
}
if s == "" {
return typ.String()
}
return s
}
// removeVendor removes everything before and including a '/vendor/'
// substring in the package path.
// This is needed becuase that full path can't be used in the
// import statement.
func removeVendor(path string) string {
i := strings.Index(path, "/vendor/")
if i == -1 {
return path
}
return path[i+8:]
}
func buildTokens(containsOptional bool, optional string, required ...string) []string {
if containsOptional {
return append(required, optional)
}
return required
}
func unquoteField(quoted bool) string {
// The outer quote of a string is already stripped out by
// the lexer. We need to check if the inner string is also
// quoted. If so, we will decode it as json string. If decoding
// fails, we will use the original string
if quoted {
return `
unquoted, ok := fflib.UnquoteBytes(outBuf)
if ok {
outBuf = unquoted
}
`
}
return ""
}
func getTmpVarFor(name string) string {
return "tmp" + strings.Replace(strings.Title(name), ".", "", -1)
}
|