1. Daniel Smith
  2. xmlm

Commits

Daniel Smith  committed ece384e

Change time handling; add CustomParse interface

  • Participants
  • Parent commits 1b1b248
  • Branches default

Comments (0)

Files changed (5)

File Makefile

View file
 GOFILES=\
 	marshal.go\
 	unmarshal.go\
+	iso8601.go\
 
 include $(GOROOT)/src/Make.pkg
 

File iso8601.go

View file
+package xmlm
+
+import (
+	"time"
+)
+
+const ISO8601 = "2006-01-02T15:04:05.000Z07:00"
+
+type ISO8601time struct {
+	time.Time
+}
+func (o ISO8601time) Name() (string, bool) {return "", false}
+func (o ISO8601time) Attributes() map[string]string {return nil}
+func (o ISO8601time) Value() (string, bool) {return o.Time.Format(ISO8601), true}
+func (o *ISO8601time) Parse(xml string) bool {
+	b := []byte(xml)
+	copy(b[20:23], []byte("000"))
+	if temp, err := time.Parse(ISO8601, string(b)); err == nil {
+		o.Time = *temp
+		return true
+	}
+	return false
+}
+	

File marshal.go

View file
 
 import (
 	"reflect"
-	"time"
 	"os"
 	"bytes"
 	"strings"
 	return x
 }
 
-func (x *xmlBuilder) timeMarshal(t *time.Time) *xmlBuilder {
-	x.b.WriteString(t.Format(time.RFC3339))
-	return x
-}
-
 func (x *xmlBuilder) builtinMarshal(number interface {}) *xmlBuilder {
 	fmt.Fprintf(&x.b, "%v", number)
 	return x
 			case *reflect.PtrType:
 				if fv := val.(*reflect.PtrValue); !fv.IsNil() {
 					switch pv := val.Interface().(type) {
-						case *time.Time:
-							x.	in(tags[0], attrs).
-								timeMarshal(pv).
-								out(tags[0])
 						default:
 							handled = false
 							val = fv.Elem()

File unmarshal.go

View file
 package xmlm
 
 import (
-	"time"	
 	"github.com/jteeuwen/go-pkg-xmlx/xmlx"
 	"reflect"
 	"os"
 	return nil
 }
 
+//Implement WantsAttributes to get your element's attributes
 type WantsAttributes interface {
 	SetAttribute(name, value string)
 }
 
+//Implement CustomParse if you want to do something special to interpret data for your type
+type CustomParse interface {
+	
+	//Parse should interpret xml and return true if successful, or false to let the unmarshaller continue
+	Parse(xml string) bool
+}
+
 //SetAttr adds an attribute to a map, creating the map if it is nil (helper function)
 func SetAttr(Attrs *map[string]string, name, value string) {
 	if *Attrs == nil {
 				}
 				u.structUnmarshal(val.(*reflect.StructValue), xn)
 			case *reflect.PtrType:
-				//if !VerifyTags() {return}
 				fv := val.(*reflect.PtrValue)
 				if fv.IsNil() {
 					zv := reflect.MakeZero(ft.Elem())
 					fv.PointTo(zv)
 				}
 				
+				if cp, ok := val.Interface().(CustomParse); ok {
+					if !VerifyTags() {return}
+					if cp.Parse(xn.GetValue(xn.Name.Space, tags[0])) {
+						return
+					}
+				}
+				
 				switch pv := val.Interface().(type) {
-					case *time.Time:
-						if !VerifyTags() {return}
-						Time, _ := time.Parse(time.RFC3339, xn.GetValue(xn.Name.Space, tags[0]))
-						fv.Set(
-							reflect.NewValue(Time).
-								(*reflect.PtrValue))
 					default:
 						handled = false
 						PtrVal = fv

File xmlm_test.go

View file
 	
 	Pi *int "pi"
 	
-	T *time.Time "t"
+	T *ISO8601time "t"
 	
 	C1 interface {} "custom1"
 	C2 interface {} "custom2"
   <b>true</b>
   <s>foo</s>
   <pi>3</pi>
-  <t>2010-09-16T09:09:10Z</t>
+  <t>2010-09-16T09:09:10.000Z</t>
   <custom1>
     <cs>bar</cs>
   </custom1>
 	"custom3": (*int)(nil),
 }
 
-var Time, _ = time.Parse(time.RFC3339, "2010-09-16T09:09:10Z")
+var _Time, _ = time.Parse(ISO8601, "2010-09-16T09:09:10.000Z")
+var Time = &ISO8601time{*_Time}
+
 var tTest = Tester{
 			I: 1,
 			F: 2.0,
 	assertTrue(tt.S == "foo", `tt.S == "foo"`, t)
 	assertTrue(tt.Pi != nil, "tt.Pi != nil", t)
 	assertTrue(*tt.Pi == 3, "*tt.Pi == 3", t)
-	assertTrue(tt.T.Seconds() == Time.Seconds(), "tt.T == Time", t)
+	//fmt.Println(tt.T, tTest.T, tt.T.Seconds(), tTest.T.Seconds())
+	assertTrue(tt.T.Seconds() == tTest.T.Seconds(), "tt.T == tTest.T", t)
 	assertTrue(tt.Ia != nil, "tt.Ia != nil", t)
 	assertTrue(len(tt.Ia) == 3, "len(tt.Ia) == 3", t)