Skip to content

Commit

Permalink
Merge pull request #4 from v8platform/encoding
Browse files Browse the repository at this point in the history
encoding
  • Loading branch information
khorevaa authored Apr 29, 2021
2 parents 6492e2d + 81fb896 commit e20a489
Show file tree
Hide file tree
Showing 14 changed files with 9,248 additions and 35 deletions.
10 changes: 0 additions & 10 deletions catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ package mdclasses

import "encoding/xml"

type ObjectTypeRef struct {
TypeId string `xml:"typeId,attr"`
ValueTypeId string `xml:"valueTypeId,attr"`
}

type ValueTypeRef struct {
Key string `xml:"key"`
Value string `xml:"value"`
}

type Catalog struct {
XMLName xml.Name `xml:"Catalog"`
Xsi string `xml:"xsi,attr"`
Expand Down
44 changes: 19 additions & 25 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,23 @@ import (
var log = logos.New("github.com/v8platform/mdclasses").Sugar()

type Configuration struct {
XMLName xml.Name `xml:"Configuration"`
Mdclass string `xml:"mdclass,attr"`
Uuid string `xml:"uuid,attr"`
Name string `xml:"name"`
Synonym struct {
Key string `xml:"key"`
Value string `xml:"value"`
} `xml:"synonym"`
XMLName xml.Name `xml:"Configuration"`
Mdclass string `xml:"mdclass,attr"`
Uuid string `xml:"uuid,attr"`
Name string `xml:"name"`
Synonym ValueTypeRef `xml:"synonym"`
ContainedObjects []struct {
ClassId string `xml:"classId,attr"`
ObjectId string `xml:"objectId,attr"`
} `xml:"containedObjects"`
ClassId string `xml:"classId,attr,allowempty"`
ObjectId string `xml:"objectId,attr,allowempty"`
} `xml:"containedObjects,allowempty"`
ConfigurationExtensionCompatibilityMode string `xml:"configurationExtensionCompatibilityMode"`
DefaultRunMode string `xml:"defaultRunMode"`
UsePurposes string `xml:"usePurposes"`
ScriptVariant string `xml:"scriptVariant"`
DefaultRoles []string `xml:"defaultRoles"`
Vendor string `xml:"vendor"`
Version string `xml:"version"`
UpdateCatalogAddress string `xml:"updateCatalogAddress"`
UpdateCatalogAddress string `xml:"updateCatalogAddress,omitempty"`
UseManagedFormInOrdinaryApplication string `xml:"useManagedFormInOrdinaryApplication"`
UseOrdinaryFormInManagedApplication string `xml:"useOrdinaryFormInManagedApplication"`
ReportsVariantsStorage string `xml:"reportsVariantsStorage"`
Expand All @@ -37,11 +34,11 @@ type Configuration struct {
DefaultSearchForm string `xml:"defaultSearchForm"`
UsedMobileApplicationFunctionalities struct {
Functionality []struct {
Functionality string `xml:"functionality,omitempty"`
Use string `xml:"use"`
Functionality string `xml:"functionality"`
} `xml:"functionality"`
} `xml:"usedMobileApplicationFunctionalities"`
MainSectionPicture string `xml:"mainSectionPicture"`
MainSectionPicture string `xml:"mainSectionPicture,allowempty"`
DefaultLanguage string `xml:"defaultLanguage"`
BriefInformation struct {
Key string `xml:"key"`
Expand All @@ -51,7 +48,7 @@ type Configuration struct {
Key string `xml:"key"`
Value string `xml:"value"`
} `xml:"detailedInformation"`
Splash string `xml:"splash"`
Splash string `xml:"splash,omitempty"`
Copyright struct {
Key string `xml:"key"`
Value string `xml:"value"`
Expand All @@ -60,16 +57,13 @@ type Configuration struct {
Key string `xml:"key"`
Value string `xml:"value"`
} `xml:"vendorInformationAddress"`
ConfigurationInformationAddress struct {
Key string `xml:"key"`
Value string `xml:"value"`
} `xml:"configurationInformationAddress"`
DataLockControlMode string `xml:"dataLockControlMode"`
ObjectAutonumerationMode string `xml:"objectAutonumerationMode"`
ModalityUseMode string `xml:"modalityUseMode"`
InterfaceCompatibilityMode string `xml:"interfaceCompatibilityMode"`
CompatibilityMode string `xml:"compatibilityMode"`
Languages struct {
ConfigurationInformationAddress ValueTypeRef `xml:"configurationInformationAddress,omitempty"`
DataLockControlMode string `xml:"dataLockControlMode"`
ObjectAutonumerationMode string `xml:"objectAutonumerationMode"`
ModalityUseMode string `xml:"modalityUseMode"`
InterfaceCompatibilityMode string `xml:"interfaceCompatibilityMode"`
CompatibilityMode string `xml:"compatibilityMode"`
Languages struct {
Uuid string `xml:"uuid,attr"`
Name string `xml:"name"`
Synonym struct {
Expand Down
56 changes: 56 additions & 0 deletions encoding/xml/atom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xml

import "time"

var atomValue = &Feed{
XMLName: Name{"http://www.w3.org/2005/Atom", "feed"},
Title: "Example Feed",
Link: []Link{{Href: "http://example.org/"}},
Updated: ParseTime("2003-12-13T18:30:02Z"),
Author: Person{Name: "John Doe"},
ID: "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6",

Entry: []Entry{
{
Title: "Atom-Powered Robots Run Amok",
Link: []Link{{Href: "http://example.org/2003/12/13/atom03"}},
ID: "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a",
Updated: ParseTime("2003-12-13T18:30:02Z"),
Summary: NewText("Some text."),
},
},
}

var atomXML = `` +
`<feed xmlns="http://www.w3.org/2005/Atom" updated="2003-12-13T18:30:02Z">` +
`<title>Example Feed</title>` +
`<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>` +
`<link href="http://example.org/"></link>` +
`<author><name>John Doe</name><uri></uri><email></email></author>` +
`<entry>` +
`<title>Atom-Powered Robots Run Amok</title>` +
`<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>` +
`<link href="http://example.org/2003/12/13/atom03"></link>` +
`<updated>2003-12-13T18:30:02Z</updated>` +
`<author><name></name><uri></uri><email></email></author>` +
`<summary>Some text.</summary>` +
`</entry>` +
`</feed>`

func ParseTime(str string) time.Time {
t, err := time.Parse(time.RFC3339, str)
if err != nil {
panic(err)
}
return t
}

func NewText(text string) Text {
return Text{
Body: text,
}
}
84 changes: 84 additions & 0 deletions encoding/xml/example_marshaling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xml_test

import (
"encoding/xml"
"fmt"
"log"
"strings"
)

type Animal int

const (
Unknown Animal = iota
Gopher
Zebra
)

func (a *Animal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
if err := d.DecodeElement(&s, &start); err != nil {
return err
}
switch strings.ToLower(s) {
default:
*a = Unknown
case "gopher":
*a = Gopher
case "zebra":
*a = Zebra
}

return nil
}

func (a Animal) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
var s string
switch a {
default:
s = "unknown"
case Gopher:
s = "gopher"
case Zebra:
s = "zebra"
}
return e.EncodeElement(s, start)
}

func Example_customMarshalXML() {
blob := `
<animals>
<animal>gopher</animal>
<animal>armadillo</animal>
<animal>zebra</animal>
<animal>unknown</animal>
<animal>gopher</animal>
<animal>bee</animal>
<animal>gopher</animal>
<animal>zebra</animal>
</animals>`
var zoo struct {
Animals []Animal `xml:"animal"`
}
if err := xml.Unmarshal([]byte(blob), &zoo); err != nil {
log.Fatal(err)
}

census := make(map[Animal]int)
for _, animal := range zoo.Animals {
census[animal] += 1
}

fmt.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n",
census[Gopher], census[Zebra], census[Unknown])

// Output:
// Zoo Census:
// * Gophers: 3
// * Zebras: 2
// * Unknown: 3
}
151 changes: 151 additions & 0 deletions encoding/xml/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xml_test

import (
"encoding/xml"
"fmt"
"os"
)

func ExampleMarshalIndent() {
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}

v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}

output, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}

os.Stdout.Write(output)
// Output:
// <person id="13">
// <name>
// <first>John</first>
// <last>Doe</last>
// </name>
// <age>42</age>
// <Married>false</Married>
// <City>Hanga Roa</City>
// <State>Easter Island</State>
// <!-- Need more details. -->
// </person>
}

func ExampleEncoder() {
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}

v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}

enc := xml.NewEncoder(os.Stdout)
enc.Indent(" ", " ")
if err := enc.Encode(v); err != nil {
fmt.Printf("error: %v\n", err)
}

// Output:
// <person id="13">
// <name>
// <first>John</first>
// <last>Doe</last>
// </name>
// <age>42</age>
// <Married>false</Married>
// <City>Hanga Roa</City>
// <State>Easter Island</State>
// <!-- Need more details. -->
// </person>
}

// This example demonstrates unmarshaling an XML excerpt into a value with
// some preset fields. Note that the Phone field isn't modified and that
// the XML <Company> element is ignored. Also, the Groups field is assigned
// considering the element path provided in its tag.
func ExampleUnmarshal() {
type Email struct {
Where string `xml:"where,attr"`
Addr string
}
type Address struct {
City, State string
}
type Result struct {
XMLName xml.Name `xml:"Person"`
Name string `xml:"FullName"`
Phone string
Email []Email
Groups []string `xml:"Group>Value"`
Address
}
v := Result{Name: "none", Phone: "none"}

data := `
<Person>
<FullName>Grace R. Emlin</FullName>
<Company>Example Inc.</Company>
<Email where="home">
<Addr>[email protected]</Addr>
</Email>
<Email where='work'>
<Addr>[email protected]</Addr>
</Email>
<Group>
<Value>Friends</Value>
<Value>Squash</Value>
</Group>
<City>Hanga Roa</City>
<State>Easter Island</State>
</Person>
`
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("XMLName: %#v\n", v.XMLName)
fmt.Printf("Name: %q\n", v.Name)
fmt.Printf("Phone: %q\n", v.Phone)
fmt.Printf("Email: %v\n", v.Email)
fmt.Printf("Groups: %v\n", v.Groups)
fmt.Printf("Address: %v\n", v.Address)
// Output:
// XMLName: xml.Name{Space:"", Local:"Person"}
// Name: "Grace R. Emlin"
// Phone: "none"
// Email: [{home [email protected]} {work [email protected]}]
// Groups: [Friends Squash]
// Address: {Hanga Roa Easter Island}
}
Loading

0 comments on commit e20a489

Please sign in to comment.