// StructType = "struct" "{" [ FieldList ] "}" .
// FieldList = Field { ";" Field } .
4995
func (p *gc_parser) parse_struct_type() ast.Expr {
	var fields []*ast.Field
	parse_field := func() {
		fld := p.parse_field()
		fields = append(fields, fld)
	}

	p.expect_keyword("struct")
	p.expect('{')
	if p.tok != '}' {
		parse_field()
		for p.tok == ';' {
			p.next()
			parse_field()
		}
	}
	p.expect('}')
	return &ast.StructType{Fields: &ast.FieldList{List: fields}}
}
75741
func ParamsStructFromJSON(value interface{}, shape *Shape, prefixPackageName bool) string {
	f := paramFiller{prefixPackageName: prefixPackageName}
	return util.GoFmt(f.paramsStructAny(value, shape))
}
1351032
func (scope *Scope) Fields() []*Field {
	if scope.fields == nil {
		var (
			fields             []*Field
			indirectScopeValue = scope.IndirectValue()
			isStruct           = indirectScopeValue.Kind() == reflect.Struct
		)

		for _, structField := range scope.GetModelStruct().StructFields {
			if isStruct {
				fieldValue := indirectScopeValue
				for _, name := range structField.Names {
					if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
						fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
					}
					fieldValue = reflect.Indirect(fieldValue).FieldByName(name)
				}
				fields = append(fields, &Field{StructField: structField, Field: fieldValue, IsBlank: isBlank(fieldValue)})
			} else {
				fields = append(fields, &Field{StructField: structField, IsBlank: true})
			}
		}
		scope.fields = &fields
	}

	return *scope.fields
}
1136512
func isEqCrossStructField(fl FieldLevel) bool {

	field := fl.Field()
	kind := field.Kind()

	topField, topKind, ok := fl.GetStructFieldOK()
	if !ok || topKind != kind {
		return false
	}

	switch kind {

	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return topField.Int() == field.Int()

	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return topField.Uint() == field.Uint()

	case reflect.Float32, reflect.Float64:
		return topField.Float() == field.Float()

	case reflect.Slice, reflect.Map, reflect.Array:
		return int64(topField.Len()) == int64(field.Len())

	case reflect.Struct:

		fieldType := field.Type()

		// Not Same underlying type i.e. struct and time
		if fieldType != topField.Type() {
			return false
		}

		if fieldType == timeType {

			t := field.Interface().(time.Time)
			fieldTime := topField.Interface().(time.Time)

			return fieldTime.Equal(t)
		}
	}

	// default reflect.String:
	return topField.String() == field.String()
}
1165181
func parseStructField(cache structCache, key, sk, keytail string, values []string, target reflect.Value) {
	l, ok := cache[sk]
	if !ok {
		panic(KeyError{
			FullKey: key,
			Key:     kpath(key, keytail),
			Type:    target.Type(),
			Field:   sk,
		})
	}
	f := target.Field(l.offset)

	l.parse(key, keytail, values, f)
}
1087714
func FlagByType(fs *flag.FlagSet, structName string, fval reflect.Value, ftype reflect.StructField) {
	// Get a pointer; FlagSet needs a pointer to set the struct's field
	if fval.Kind() == reflect.Ptr {
		// Short-circuit
		log.Printf("Skipping field %s: %s", ftype.Name, ftype.Type.String())
		return
	}
	//log.Printf("Getting pointer to %s", ftype.Name)
	fval = fval.Addr()
	flagName := NameToFlag(ftype.Name)
	flagHelp := fmt.Sprintf("%s:%s", structName, ftype.Name)
	log.Printf("Converting %s => %s", ftype.Name, flagName)

	//log.Printf("Switching on type %s...", ftype.Type.String())
	switch fval := fval.Interface().(type) {
	case *int:
		fs.IntVar(fval, flagName, 0, flagHelp)
	case *float64:
		fs.Float64Var(fval, flagName, 0.0, flagHelp)
	case *string:
		fs.StringVar(fval, flagName, "", flagHelp)
	case *bool:
		fs.BoolVar(fval, flagName, false, flagHelp)
	case *time.Time:
		t := (*time.Time)(fval) // Get a *time.Time pointer to fval
		*t = time.Now()         // Set a default of time.Now()
		fs.Var((*TimeFlag)(fval), flagName, flagHelp)
	default:
		log.Printf("unexpected type %s\n", ftype.Type.String())
	}
}
961620
func ValidateStructField(target interface{}, field string) (bool, error) {
	val := reflect.ValueOf(target)

	if val.Kind() == reflect.Ptr {
		val = val.Elem()
	}

	valueField := val.FieldByName(field)
	typeField, ok := val.Type().FieldByName(field)
	if !ok {
		return false, fmt.Errorf("unknown field %s", field)
	}

	validation := strings.TrimSpace(typeField.Tag.Get("validate"))

	err := validateStructField(valueField, typeField, validation)
	if err != nil {
		return false, err
	}

	return true, nil
}
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,
					)
				}
			}
		}
	}
}
761181
func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, error) {
	finfo := &fieldInfo{idx: f.Index}

	// Split the tag from the xml namespace if necessary.
	tag := f.Tag.Get("xml")
	if i := strings.Index(tag, " "); i >= 0 {
		finfo.xmlns, tag = tag[:i], tag[i+1:]
	}

	// Parse flags.
	tokens := strings.Split(tag, ",")
	if len(tokens) == 1 {
		finfo.flags = fElement
	} else {
		tag = tokens[0]
		for _, flag := range tokens[1:] {
			switch flag {
			case "attr":
				finfo.flags |= fAttr
			case "chardata":
				finfo.flags |= fCharData
			case "innerxml":
				finfo.flags |= fInnerXml
			case "comment":
				finfo.flags |= fComment
			case "any":
				finfo.flags |= fAny
			case "omitempty":
				finfo.flags |= fOmitEmpty
			case "typeattr":
				finfo.flags |= fTypeAttr
			}
		}

		// Validate the flags used.
		valid := true
		switch mode := finfo.flags & fMode; mode {
		case 0:
			finfo.flags |= fElement
		case fAttr, fCharData, fInnerXml, fComment, fAny:
			if f.Name == "XMLName" || tag != "" && mode != fAttr {
				valid = false
			}
		default:
			// This will also catch multiple modes in a single field.
			valid = false
		}
		if finfo.flags&fMode == fAny {
			finfo.flags |= fElement
		}
		if finfo.flags&fOmitEmpty != 0 && finfo.flags&(fElement|fAttr) == 0 {
			valid = false
		}
		if !valid {
			return nil, fmt.Errorf("xml: invalid tag in field %s of type %s: %q",
				f.Name, typ, f.Tag.Get("xml"))
		}
	}

	// Use of xmlns without a name is not allowed.
	if finfo.xmlns != "" && tag == "" {
		return nil, fmt.Errorf("xml: namespace without name in field %s of type %s: %q",
			f.Name, typ, f.Tag.Get("xml"))
	}

	if f.Name == "XMLName" {
		// The XMLName field records the XML element name. Don't
		// process it as usual because its name should default to
		// empty rather than to the field name.
		finfo.name = tag
		return finfo, nil
	}

	if tag == "" {
		// If the name part of the tag is completely empty, get
		// default from XMLName of underlying struct if feasible,
		// or field name otherwise.
		if xmlname := lookupXMLName(f.Type); xmlname != nil {
			finfo.xmlns, finfo.name = xmlname.xmlns, xmlname.name
		} else {
			finfo.name = f.Name
		}
		return finfo, nil
	}

	// Prepare field name and parents.
	parents := strings.Split(tag, ">")
	if parents[0] == "" {
		parents[0] = f.Name
	}
	if parents[len(parents)-1] == "" {
		return nil, fmt.Errorf("xml: trailing '>' in field %s of type %s", f.Name, typ)
	}
	finfo.name = parents[len(parents)-1]
	if len(parents) > 1 {
		if (finfo.flags & fElement) == 0 {
			return nil, fmt.Errorf("xml: %s chain not valid with %s flag", tag, strings.Join(tokens[1:], ","))
		}
		finfo.parents = parents[:len(parents)-1]
	}

	// If the field type has an XMLName field, the names must match
	// so that the behavior of both marshalling and unmarshalling
	// is straightforward and unambiguous.
	if finfo.flags&fElement != 0 {
		ftyp := f.Type
		xmlname := lookupXMLName(ftyp)
		if xmlname != nil && xmlname.name != finfo.name {
			return nil, fmt.Errorf("xml: name %q in tag of %s.%s conflicts with name %q in %s.XMLName",
				finfo.name, typ, f.Name, xmlname.name, ftyp)
		}
	}
	return finfo, nil
}
1321709
func GetStructType(alias string) (structType reflect.Type) {
	structTypesLocker.RLock()
	structType = structTypes[alias]
	structTypesLocker.RUnlock()
	return structType
}
946220
func structToMapString(i interface{}) map[string][]string {
	ms := map[string][]string{}
	iv := reflect.ValueOf(i).Elem()
	tp := iv.Type()

	for i := 0; i < iv.NumField(); i++ {
		k := tp.Field(i).Name
		f := iv.Field(i)
		ms[k] = valueToString(f)
	}

	return ms
}
1107535
func (f paramFiller) paramsStructList(value []interface{}, shape *Shape) string {
	out := f.typeName(shape) + "{\n"
	for _, v := range value {
		out += fmt.Sprintf("%s,\n", f.paramsStructAny(v, shape.MemberRef.Shape))
	}
	out += "}"
	return out
}
116262
func (schema *Schema) Marshal(s interface{}) ([]byte, error) {
	v := reflect.ValueOf(s)

	if v.Kind() == reflect.PtrTo(reflect.TypeOf(s)).Kind() {
		v = reflect.Indirect(v)
	}

	// Only structs are supported so return an empty result if the passed object
	// isn't a struct.
	if v.Kind() != reflect.Struct {
		return nil, ErrUnsupportedType(v.Kind())
	}

	// If a pointer to a struct is passed, get the type of the dereferenced object.
	typ := reflect.TypeOf(s)
	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
	}

	// Check for unsupported interface types
	err := checkFieldType(v, typ)
	if err != nil {
		return nil, err
	}

	avroSchema, err := avro.ParseSchema(schema.Definition)
	if err != nil {
		return nil, err
	}

	record, err := getRecord(avroSchema, v, typ)
	if err != nil {
		return nil, err
	}

	writer := avro.NewGenericDatumWriter()
	writer.SetSchema(avroSchema)

	buffer := new(bytes.Buffer)
	encoder := avro.NewBinaryEncoder(buffer)

	err = writer.Write(record, encoder)
	if err != nil {
		return nil, err
	}

	return buffer.Bytes(), nil
}
1093605
func fieldNames(t reflect.Type) map[string]struct{} {
	for ; t.Kind() == reflect.Ptr; t = t.Elem() {
	}

	if t.Kind() != reflect.Struct {
		return nil
	}

	fields := make(map[string]struct{}, t.NumField())
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		if field.PkgPath != "" {
			continue // unexported field
		}
		fields[field.Name] = struct{}{}
	}
	return fields
}
546732
func AllocStructPtrs(val reflect.Value) {
	switch val.Kind() {
	case reflect.Struct:
		for i := 0; i < val.Type().NumField(); i++ {
			field := val.Field(i)
			AllocStructPtrs(field)
		}
	case reflect.Ptr:
		v := MakeNewType(val.Type(), 0)
		//TODO: Make recursivy better, include val.Type == v.Type....
		if isRecursive(v) {
			//panic(fmt.Sprintf("struct %v have a field of the same type of this struct", NameOf(v.Type())))
			return
		}
		if val.Type() == v.Type() {
			return
		}
		AllocStructPtrs(v.Elem())
		if val.CanSet() {
			val.Set(v)
		}
	case reflect.Slice:
		v := MakeNewType(val.Type(), 0)
		elem := v.Type().Elem()
		if elem.Kind() == reflect.Ptr && elem.Elem() == v.Type() {
			return
		}
		if val.CanSet() {
			val.Set(v)
		}
	}
}
616101
func (f *FilterOp) Type(fieldType string) *FilterOp {
	f.TypeProp = &TypeFilter{Value: fieldType}
	return f
}
872534
func Struct(name string, opts ...FieldOpt) FieldOpt {
	return func(em *eventMetadata, ed *eventData) {
		em.writeStruct(name, uint8(len(opts)), 0)
		for _, opt := range opts {
			opt(em, ed)
		}
	}
}