| // Go support for Protocol Buffers - Google's data interchange format |
| // |
| // Copyright 2010 Google Inc. All rights reserved. |
| // http://code.google.com/p/goprotobuf/ |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are |
| // met: |
| // |
| // * Redistributions of source code must retain the above copyright |
| // notice, this list of conditions and the following disclaimer. |
| // * Redistributions in binary form must reproduce the above |
| // copyright notice, this list of conditions and the following disclaimer |
| // in the documentation and/or other materials provided with the |
| // distribution. |
| // * Neither the name of Google Inc. nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| package proto |
| |
| // Functions for writing the text protocol buffer format. |
| // TODO: message sets. |
| |
| import ( |
| "bytes" |
| "fmt" |
| "io" |
| "log" |
| "os" |
| "reflect" |
| "strconv" |
| "strings" |
| ) |
| |
| // textWriter is an io.Writer that tracks its indentation level. |
| type textWriter struct { |
| ind int |
| complete bool // if the current position is a complete line |
| compact bool // whether to write out as a one-liner |
| writer io.Writer |
| |
| c [1]byte // scratch |
| } |
| |
| func (w *textWriter) Write(p []byte) (n int, err os.Error) { |
| n, err = len(p), nil |
| |
| frags := strings.Split(string(p), "\n") |
| if w.compact { |
| w.writer.Write([]byte(strings.Join(frags, " "))) |
| return |
| } |
| |
| for i, frag := range frags { |
| if w.complete { |
| for j := 0; j < w.ind; j++ { |
| w.writer.Write([]byte{' ', ' '}) |
| } |
| w.complete = false |
| } |
| |
| w.writer.Write([]byte(frag)) |
| if i+1 < len(frags) { |
| w.writer.Write([]byte{'\n'}) |
| } |
| } |
| w.complete = len(frags[len(frags)-1]) == 0 |
| |
| return |
| } |
| |
| func (w *textWriter) WriteByte(c byte) os.Error { |
| w.c[0] = c |
| _, err := w.Write(w.c[:]) |
| return err |
| } |
| |
| func (w *textWriter) indent() { w.ind++ } |
| |
| func (w *textWriter) unindent() { |
| if w.ind == 0 { |
| log.Printf("proto: textWriter unindented too far!") |
| return |
| } |
| w.ind-- |
| } |
| |
| func writeName(w *textWriter, props *Properties) { |
| io.WriteString(w, props.OrigName) |
| if props.Wire != "group" { |
| w.WriteByte(':') |
| } |
| } |
| |
| var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem() |
| |
| func writeStruct(w *textWriter, sv reflect.Value) { |
| st := sv.Type() |
| sprops := GetProperties(st) |
| for i := 0; i < sv.NumField(); i++ { |
| if strings.HasPrefix(st.Field(i).Name, "XXX_") { |
| continue |
| } |
| props := sprops.Prop[i] |
| fv := sv.Field(i) |
| if fv.Kind() == reflect.Ptr && fv.IsNil() { |
| // Field not filled in. This could be an optional field or |
| // a required field that wasn't filled in. Either way, there |
| // isn't anything we can show for it. |
| continue |
| } |
| if fv.Kind() == reflect.Slice && fv.IsNil() { |
| // Repeated field that is empty, or a bytes field that is unused. |
| continue |
| } |
| |
| if props.Repeated && fv.Kind() == reflect.Slice { |
| // Repeated field. |
| for j := 0; j < fv.Len(); j++ { |
| writeName(w, props) |
| if !w.compact { |
| w.WriteByte(' ') |
| } |
| writeAny(w, fv.Index(j), props) |
| w.WriteByte('\n') |
| } |
| continue |
| } |
| |
| writeName(w, props) |
| if !w.compact { |
| w.WriteByte(' ') |
| } |
| if props.Enum != "" && tryWriteEnum(w, props.Enum, fv) { |
| // Enum written. |
| } else { |
| writeAny(w, fv, props) |
| } |
| w.WriteByte('\n') |
| } |
| |
| // Extensions. |
| pv := sv.Addr() |
| if pv.Type().Implements(extendableProtoType) { |
| writeExtensions(w, pv) |
| } |
| } |
| |
| // tryWriteEnum attempts to write an enum value as a symbolic constant. |
| // If the enum is unregistered, nothing is written and false is returned. |
| func tryWriteEnum(w *textWriter, enum string, v reflect.Value) bool { |
| v = reflect.Indirect(v) |
| if v.Type().Kind() != reflect.Int32 { |
| return false |
| } |
| m, ok := enumNameMaps[enum] |
| if !ok { |
| return false |
| } |
| str, ok := m[int32(v.Int())] |
| if !ok { |
| return false |
| } |
| fmt.Fprintf(w, str) |
| return true |
| } |
| |
| // writeAny writes an arbitrary field. |
| func writeAny(w *textWriter, v reflect.Value, props *Properties) { |
| v = reflect.Indirect(v) |
| |
| // We don't attempt to serialise every possible value type; only those |
| // that can occur in protocol buffers, plus a few extra that were easy. |
| switch v.Kind() { |
| case reflect.Slice: |
| // Should only be a []byte; repeated fields are handled in writeStruct. |
| // TODO: Should be strconv.QuoteToASCII, which should be released after 2011-06-20. |
| fmt.Fprint(w, strconv.Quote(string(v.Interface().([]byte)))) |
| case reflect.String: |
| // TODO: Should be strconv.QuoteToASCII, which should be released after 2011-06-20. |
| fmt.Fprint(w, strconv.Quote(v.String())) |
| case reflect.Struct: |
| // Required/optional group/message. |
| var bra, ket byte = '<', '>' |
| if props != nil && props.Wire == "group" { |
| bra, ket = '{', '}' |
| } |
| w.WriteByte(bra) |
| if !w.compact { |
| w.WriteByte('\n') |
| } |
| w.indent() |
| writeStruct(w, v) |
| w.unindent() |
| w.WriteByte(ket) |
| default: |
| fmt.Fprint(w, v.Interface()) |
| } |
| } |
| |
| // writeExtensions writes all the extensions in pv. |
| // pv is assumed to be a pointer to a protocol message struct that is extendable. |
| func writeExtensions(w *textWriter, pv reflect.Value) { |
| emap := extensionMaps[pv.Type().Elem()] |
| ep := pv.Interface().(extendableProto) |
| for extNum := range ep.ExtensionMap() { |
| var desc *ExtensionDesc |
| if emap != nil { |
| desc = emap[extNum] |
| } |
| if desc == nil { |
| // TODO: Handle printing unknown extensions. |
| fmt.Fprintln(os.Stderr, "proto: unknown extension: ", extNum) |
| continue |
| } |
| |
| pb, err := GetExtension(ep, desc) |
| if err != nil { |
| fmt.Fprintln(os.Stderr, "proto: failed getting extension: ", err) |
| continue |
| } |
| |
| fmt.Fprintf(w, "[%s]:", desc.Name) |
| if !w.compact { |
| w.WriteByte(' ') |
| } |
| writeAny(w, reflect.ValueOf(pb), nil) |
| w.WriteByte('\n') |
| } |
| } |
| |
| func marshalText(w io.Writer, pb interface{}, compact bool) { |
| if pb == nil { |
| w.Write([]byte("<nil>")) |
| return |
| } |
| aw := new(textWriter) |
| aw.writer = w |
| aw.complete = true |
| aw.compact = compact |
| |
| v := reflect.ValueOf(pb) |
| // We should normally be passed a struct, or a pointer to a struct, |
| // and we don't want the outer < and > in that case. |
| v = reflect.Indirect(v) |
| if v.Kind() == reflect.Struct { |
| writeStruct(aw, v) |
| } else { |
| writeAny(aw, v, nil) |
| } |
| } |
| |
| // MarshalText writes a given protocol buffer in text format. |
| // Values that are not protocol buffers can also be written, but their formatting is not guaranteed. |
| func MarshalText(w io.Writer, pb interface{}) { marshalText(w, pb, false) } |
| |
| // CompactText writes a given protocl buffer in compact text format (one line). |
| // Values that are not protocol buffers can also be written, but their formatting is not guaranteed. |
| func CompactText(w io.Writer, pb interface{}) { marshalText(w, pb, true) } |
| |
| // CompactTextString is the same as CompactText, but returns the string directly. |
| func CompactTextString(pb interface{}) string { |
| buf := new(bytes.Buffer) |
| marshalText(buf, pb, true) |
| return buf.String() |
| } |