// VerifyJSONStruct Example:
//   VerifyJSONStruct(t, json)
191660
func VerifyJSONStruct(t Failable, obj interface{}) error {
	jsonb, err := json.MarshalIndent(obj, "", "  ")
	if err != nil {
		message := fmt.Sprintf("error while pretty printing JSON\nerror:\n  %s\nJSON:\n  %s\n", err, obj)
		return VerifyWithExtension(t, strings.NewReader(message), ".json")
	}

	return VerifyWithExtension(t, bytes.NewReader(jsonb), ".json")
}
1019930
func serializeStruct(src interface{}) ([]byte, error) {
	if src == nil {
		return serializeProperties(nil, false)
	}
	if k := reflect.Indirect(reflect.ValueOf(src)).Type().Kind(); k != reflect.Struct {
		return nil, fmt.Errorf("goon: Expected struct, got instead: %v", k)
	}

	var err error
	var props []datastore.Property
	if pls, ok := src.(datastore.PropertyLoadSaver); ok {
		props, err = pls.Save()
	} else {
		props, err = datastore.SaveStruct(src)
	}
	if err != nil {
		return nil, err
	}

	return serializeProperties(props, true)
}
693050
func (ref *Ref) Verify(serializedID []byte, msg []byte, sig []byte) error {
	membership, err := ref.get()
	if err != nil {
		return err
	}
	return membership.Verify(serializedID, msg, sig)
}
703324
func NewStructTypeDef(init ...*StructTypeDef) *StructTypeDef {
	var o *StructTypeDef
	if len(init) == 1 {
		o = init[0]
	} else {
		o = new(StructTypeDef)
	}
	return o.Init()
}
516928
func IsStruct(obj interface{}) bool {
	return obj != nil && reflect.TypeOf(obj).Kind() == reflect.Struct
}
8276
func NewStructFieldDef(init ...*StructFieldDef) *StructFieldDef {
	var o *StructFieldDef
	if len(init) == 1 {
		o = init[0]
	} else {
		o = new(StructFieldDef)
	}
	return o
}
232451
func verifyJSON(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	var echoReq *EchoRequest
	err := json.NewDecoder(r.Body).Decode(&echoReq)
	if err != nil {
		HTTPError(w, err.Error(), "Bad Request", 400)
		return
	}

	// Check the timestamp
	if !echoReq.VerifyTimestamp() && r.URL.Query().Get("_dev") == "" {
		HTTPError(w, "Request too old to continue (>150s).", "Bad Request", 400)
		return
	}

	// Check the app id
	if !echoReq.VerifyAppID(applications[r.URL.Path].(EchoApplication).AppID) {
		HTTPError(w, "Echo AppID mismatch!", "Bad Request", 400)
		return
	}

	r = r.WithContext(context.WithValue(r.Context(), requestContextKey("echoRequest"), echoReq))

	next(w, r)
}
166248
func (v *Validator) CheckJSONSchema() (errs error) {
	logrus.Debugf("check JSON schema")

	url, err := JSONSchemaURL(v.spec.Version)
	if err != nil {
		errs = multierror.Append(errs, err)
		return errs
	}

	schemaLoader := gojsonschema.NewReferenceLoader(url)
	documentLoader := gojsonschema.NewGoLoader(v.spec)
	result, err := gojsonschema.Validate(schemaLoader, documentLoader)
	if err != nil {
		errs = multierror.Append(errs, err)
		return errs
	}

	if !result.Valid() {
		for _, resultError := range result.Errors() {
			errs = multierror.Append(errs, errors.New(resultError.String()))
		}
	}

	return errs
}
571958
func (me *TxDatabase) ScanStruct(i interface{}, query string, args ...interface{}) (bool, error) {
	exec := newCrudExec(me, nil, query, args...)
	return exec.ScanStruct(i)
}
545687
func StringJson(v interface{}, unq bool) (ret string) {
	ret = simplejson.MustDumpString(v)
	if unq {
		ret, _ = unquote(strings.TrimSpace(ret), '"')
	}

	return
}
4486
func (m *SignedMessage) Verify(verifier Signer) (bool, error) {
	if verifier == nil {
		return false, errors.New("nil verifier")
	}

	sig := m.Sig
	m.Sig = nil
	defer func() {
		m.Sig = sig
	}()

	bytes, err := json.Marshal(m)
	if err != nil {
		return false, errors.Wrap(err, "sign error: json.Marshal returned")
	}

	return verifier.Verify(sig, bytes)
}
814088
func getStruct(s interface{}) reflect.Value {
	in := reflect.Indirect(reflect.ValueOf(s))
	if in.Kind() != reflect.Struct {
		panic(fmt.Sprintf("s must be a sturct, not %s\n", in.Kind()))
	}
	return in
}
1073445
func VerifyAll(t Failable, header string, collection interface{}, transform func(interface{}) string) error {
	if len(header) != 0 {
		header = fmt.Sprintf("%s\n\n\n", header)
	}

	outputText := header + strings.Join(utils.MapToString(collection, transform), "\n")
	return VerifyString(t, outputText)
}
748184
func parseStructInfo(stag string) (toArray, omitEmpty bool, keytype valueType) {
	keytype = valueTypeString // default
	if stag == "" {
		return
	}
	for i, s := range strings.Split(stag, ",") {
		if i == 0 {
		} else {
			switch s {
			case "omitempty":
				omitEmpty = true
			case "toarray":
				toArray = true
			case "int":
				keytype = valueTypeInt
			case "uint":
				keytype = valueTypeUint
			case "float":
				keytype = valueTypeFloat
				// case "bool":
				// 	keytype = valueTypeBool
			case "string":
				keytype = valueTypeString
			}
		}
	}
	return
}
1258134
func EnsureBindStruct(ctx martini.Context, obj interface{}) reflect.Value {
	objType := ctx.Get(reflect.TypeOf(obj))
	if objType.IsValid() {
		return objType
	}

	ctx.Invoke(BindStruct(obj))
	objType = ctx.Get(reflect.TypeOf(obj))
	return objType
}
1188236
func DynamicJSONToStruct(data interface{}, target interface{}) error {
	// TODO: convert straight to a json typed map  (mergo + iterate?)
	b, err := WriteJSON(data)
	if err != nil {
		return err
	}
	return ReadJSON(b, target)
}
29228
func AnalyzeStructParams(
	requestStruct interface{},
	funcArgs *RequestParams,
	params *RequestParams,
	extraStructs *ExtraStructs,
) {
	reflectStruct, ok := requestStruct.(reflect.Type)
	if !ok {
		reflectStruct = reflect.TypeOf(requestStruct)
	}
	for i := 0; i < reflectStruct.NumField(); i++ {
		field := reflectStruct.Field(i)

		if fieldTag := field.Tag.Get("apijs"); fieldTag == "inherit" {
			AnalyzeStructParams(field.Type, funcArgs, params, extraStructs)
			continue
		}

		if fieldTag := field.Tag.Get("json"); fieldTag != "" {
			if tags := strings.Split(fieldTag, ","); len(tags) > 0 {
				jsonName := tags[0]
				if jsonName == "-" {
					continue
				}

				funcArgs.Upsert(jsonName, &field)
				params.Upsert(jsonName, &field)

				structType := ensureReflectStruct(field.Type)
				switch structType.Kind() {
				case reflect.Struct:
					extraStruct := extraStructs.Upsert(jsonName, field.Type)
					AnalyzeStructParams(
						structType,
						&extraStruct.Params,
						nil,
						extraStructs,
					)
				}
			}
		}
	}
}