1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // MarshalJSON returns a JSON encoding of schema containing only selected fields.
15 // A field is selected if any of the following is true:
16 // * it has a non-empty value
17 // * its field name is present in forceSendFields and it is not a nil pointer or nil interface
18 // * its field name is present in nullFields.
19 // The JSON key for each selected field is taken from the field's json: struct tag.
20 func MarshalJSON(schema interface{}, forceSendFields, nullFields []string) ([]byte, error) {
21 if len(forceSendFields) == 0 && len(nullFields) == 0 {
22 return json.Marshal(schema)
25 mustInclude := make(map[string]bool)
26 for _, f := range forceSendFields {
29 useNull := make(map[string]bool)
30 useNullMaps := make(map[string]map[string]bool)
31 for _, nf := range nullFields {
32 parts := strings.SplitN(nf, ".", 2)
37 if useNullMaps[field] == nil {
38 useNullMaps[field] = map[string]bool{}
40 useNullMaps[field][parts[1]] = true
44 dataMap, err := schemaToMap(schema, mustInclude, useNull, useNullMaps)
48 return json.Marshal(dataMap)
51 func schemaToMap(schema interface{}, mustInclude, useNull map[string]bool, useNullMaps map[string]map[string]bool) (map[string]interface{}, error) {
52 m := make(map[string]interface{})
53 s := reflect.ValueOf(schema)
56 for i := 0; i < s.NumField(); i++ {
57 jsonTag := st.Field(i).Tag.Get("json")
61 tag, err := parseJSONTag(jsonTag)
74 return nil, fmt.Errorf("field %q in NullFields has non-empty value", f.Name)
80 if !includeField(v, f, mustInclude) {
84 // If map fields are explicitly set to null, use a map[string]interface{}.
85 if f.Type.Kind() == reflect.Map && useNullMaps[f.Name] != nil {
86 ms, ok := v.Interface().(map[string]string)
88 return nil, fmt.Errorf("field %q has keys in NullFields but is not a map[string]string", f.Name)
90 mi := map[string]interface{}{}
91 for k, v := range ms {
94 for k := range useNullMaps[f.Name] {
101 // nil maps are treated as empty maps.
102 if f.Type.Kind() == reflect.Map && v.IsNil() {
103 m[tag.apiName] = map[string]string{}
107 // nil slices are treated as empty slices.
108 if f.Type.Kind() == reflect.Slice && v.IsNil() {
109 m[tag.apiName] = []bool{}
113 if tag.stringFormat {
114 m[tag.apiName] = formatAsString(v, f.Type.Kind())
116 m[tag.apiName] = v.Interface()
122 // formatAsString returns a string representation of v, dereferencing it first if possible.
123 func formatAsString(v reflect.Value, kind reflect.Kind) string {
124 if kind == reflect.Ptr && !v.IsNil() {
128 return fmt.Sprintf("%v", v.Interface())
131 // jsonTag represents a restricted version of the struct tag format used by encoding/json.
132 // It is used to describe the JSON encoding of fields in a Schema struct.
133 type jsonTag struct {
139 // parseJSONTag parses a restricted version of the struct tag format used by encoding/json.
140 // The format of the tag must match that generated by the Schema.writeSchemaStruct method
141 // in the api generator.
142 func parseJSONTag(val string) (jsonTag, error) {
144 return jsonTag{ignore: true}, nil
149 i := strings.Index(val, ",")
150 if i == -1 || val[:i] == "" {
151 return tag, fmt.Errorf("malformed json tag: %s", val)
160 case "omitempty,string":
161 tag.stringFormat = true
163 return tag, fmt.Errorf("malformed json tag: %s", val)
169 // Reports whether the struct field "f" with value "v" should be included in JSON output.
170 func includeField(v reflect.Value, f reflect.StructField, mustInclude map[string]bool) bool {
171 // The regular JSON encoding of a nil pointer is "null", which means "delete this field".
172 // Therefore, we could enable field deletion by honoring pointer fields' presence in the mustInclude set.
173 // However, many fields are not pointers, so there would be no way to delete these fields.
174 // Rather than partially supporting field deletion, we ignore mustInclude for nil pointer fields.
175 // Deletion will be handled by a separate mechanism.
176 if f.Type.Kind() == reflect.Ptr && v.IsNil() {
180 // The "any" type is represented as an interface{}. If this interface
181 // is nil, there is no reasonable representation to send. We ignore
182 // these fields, for the same reasons as given above for pointers.
183 if f.Type.Kind() == reflect.Interface && v.IsNil() {
187 return mustInclude[f.Name] || !isEmptyValue(v)
190 // isEmptyValue reports whether v is the empty value for its type. This
191 // implementation is based on that of the encoding/json package, but its
192 // correctness does not depend on it being identical. What's important is that
193 // this function return false in situations where v should not be sent as part
194 // of a PATCH operation.
195 func isEmptyValue(v reflect.Value) bool {
197 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
201 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
203 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
205 case reflect.Float32, reflect.Float64:
206 return v.Float() == 0
207 case reflect.Interface, reflect.Ptr: