Migrate all libvirt calls to the generated code.

- Update libvirt.go so that all libvirt calls now go through the
generated routines.
- Remove some libvirt routines that had the same name as generated ones,
leave the rest as convenience routines.
- Fix the handling of Optional-values (the declarations of which in the
.x file look like pointers)
This commit is contained in:
Geoff Hickey
2017-11-16 14:17:46 -05:00
parent a3bd42a5b1
commit b59e9d42f4
7 changed files with 297 additions and 1101 deletions

View File

@@ -25,6 +25,10 @@ import (
"unicode/utf8"
)
// If you're making changes to the generator, or troubleshooting the generated
// code, the docs for sunrpc and xdr (the line encoding) are helpful:
// https://docs.oracle.com/cd/E26502_01/html/E35597/
// ConstItem stores an const's symbol and value from the parser. This struct is
// also used for enums.
type ConstItem struct {
@@ -81,7 +85,6 @@ var goEquivTypes = map[string]string{
// TODO: Get rid of these. They're only needed because we lose information
// that the parser has (the parser knows it has emitted a go type), and then
// we capitalize types to make them public.
"String": "string",
"Int": "int",
"Uint": "uint",
"Int8": "int8",
@@ -580,7 +583,7 @@ func checkIdentifier(i string) string {
}
// GetUnion returns the type information for a union. If the provided type name
// isn't a union, the second return value will be false.
// isn't a union, this will return a zero-value Union type.
func (decl *Decl) GetUnion() Union {
ix, ok := Gen.UnionMap[decl.Type]
if ok {

View File

@@ -42,8 +42,10 @@ type {{$casetype}} struct {
DVal uint32
{{.Name}} {{.Type}}
}
func New{{$casetype}}(v {{.Type}}) *{{$casetype}} { return &{{$casetype}}{DVal: {{.DiscriminantVal}}, {{.Name}}: v} }
func Decode{{$casetype}}(dec *xdr.Decoder) (*{{$casetype}}, error) {
func New{{$casetype}}(v {{.Type}}) *{{$casetype}} {
return &{{$casetype}}{DVal: {{.DiscriminantVal}}, {{.Name}}: v}
}
func decode{{$casetype}}(dec *xdr.Decoder) (*{{$casetype}}, error) {
var v {{.Type}}
_, err := dec.Decode(&v)
if err != nil {
@@ -53,49 +55,46 @@ func Decode{{$casetype}}(dec *xdr.Decoder) (*{{$casetype}}, error) {
}
func (c *{{$casetype}}) Get() interface{} { return c.{{.Name}} }
{{end}}
{{- end}}
// TODO: Generate this.
func decodeTypedParams(dec *xdr.Decoder) ([]TypedParam, error) {
count, _, err := dec.DecodeInt()
params := make([]TypedParam, count)
func decode{{.Name}}(dec *xdr.Decoder) ({{.Name}}, error) {
discriminant, _, err := dec.DecodeInt()
if err != nil {
return nil, err
}
var caseval {{.Name}}
switch discriminant {
{{range .Cases}}{{$casetype := printf "%v%v" $uname .CaseName}} case {{.DiscriminantVal}}:
caseval, err = decode{{$casetype}}(dec)
{{end}}
default:
err = fmt.Errorf("invalid parameter type %v", discriminant)
}
return caseval, err
}
{{- end}}
// TODO: Generate these.
func decodeTypedParam(dec *xdr.Decoder) (*TypedParam, error) {
name, _, err := dec.DecodeString()
if err != nil {
return nil, err
}
val, err := decodeTypedParamValue(dec)
return &TypedParam{name, val}, nil
}
func decodeTypedParams(dec *xdr.Decoder) ([]TypedParam, error) {
count, _, err := dec.DecodeInt()
if err != nil {
return nil, err
}
params := make([]TypedParam, count)
for ix := int32(0); ix < count; ix++ {
name, _, err := dec.DecodeString()
p, err := decodeTypedParam(dec)
if err != nil {
return nil, err
}
ptype, _, err := dec.DecodeInt()
if err != nil {
return nil, err
}
var tpv TypedParamValue
switch ptype {
case 1: // TypedParamValueInt
tpv, err = DecodeTypedParamValueInt(dec)
case 2: // TypedParamValueUint
tpv, err = DecodeTypedParamValueUint(dec)
case 3: // TypedParamValueLlong
tpv, err = DecodeTypedParamValueLlong(dec)
case 4: // TypedParamValueUllong
tpv, err = DecodeTypedParamValueUllong(dec)
case 5: // TypedParamValueDouble
tpv, err = DecodeTypedParamValueDouble(dec)
case 6: // TypedParamValueBoolean
tpv, err = DecodeTypedParamValueBoolean(dec)
case 7: // TypedParamValueString
tpv, err = DecodeTypedParamValueString(dec)
default:
err = fmt.Errorf("invalid parameter type %v", ptype)
}
if err != nil {
return nil, err
}
params[ix] = TypedParam{name, tpv}
params[ix] = *p
}
return params, nil
@@ -131,17 +130,16 @@ func (l *Libvirt) {{.Name}}({{range $ix, $arg := .Args}}{{if $ix}}, {{end}}{{.Na
rdr := bytes.NewReader(r.Payload)
dec := xdr.NewDecoder(rdr)
{{range .Ret}} // {{.Name}}: {{.Type}}
{{if eq .Type "[]TypedParam"}} // {{.Name}}
r{{.Name}}, err = decodeTypedParams(dec)
{{if eq .Type "[]TypedParam"}} r{{.Name}}, err = decodeTypedParams(dec)
if err != nil {
fmt.Println("error decoding typedparams")
return
}
{{else}} _, err = dec.Decode(&r{{.Name}})
if err != nil {
return
}
{{end}}{{end}}
{{end}}
{{end}}{{end}}{{end}}
return
}
{{end}}

View File

@@ -146,8 +146,12 @@ variable_array_declaration
| type_specifier variable_ident '<' '>' { AddVariableArray($2.val, $1.val, "") }
;
// while pointer_declarations may look like their familiar c-equivalents, in the
// XDR language they actually declare "Optional-data". The simplest
// representation to use for these is a variable-length array with a size of 1.
// See the XDR spec for a more complete explanation of this.
pointer_declaration
: type_specifier '*' variable_ident { AddDeclaration($3.val, "*"+$1.val) }
: type_specifier '*' variable_ident { AddVariableArray($3.val, $1.val, "1") }
;
struct_definition