diff options
author | Jens Geyer <jensg@apache.org> | 2013-12-17 21:38:44 +0100 |
---|---|---|
committer | Jens Geyer <jensg@apache.org> | 2013-12-17 21:38:44 +0100 |
commit | 0853ab67b4db1db15dc4b8f195764331174e5594 (patch) | |
tree | 213f4b1bf8ff1336e802b08c31a3c3224039dfe9 /lib/go | |
parent | 2c4f306b0a93454b7e18aa425e4e8f7f4f9ef4f5 (diff) | |
download | thrift-0853ab67b4db1db15dc4b8f195764331174e5594.tar.gz |
THRIFT-2232 IsSet* broken in Go
Patch: Ben Sigelman
Diffstat (limited to 'lib/go')
-rw-r--r-- | lib/go/README | 39 | ||||
-rw-r--r-- | lib/go/thrift/pointerize.go | 48 |
2 files changed, 85 insertions, 2 deletions
diff --git a/lib/go/README b/lib/go/README index 94628d9de..87ba6f903 100644 --- a/lib/go/README +++ b/lib/go/README @@ -1,4 +1,4 @@ -Thrift Python Software Library +Thrift Go Software Library License ======= @@ -20,12 +20,47 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + Using Thrift with Go ==================== In following Go conventions, we reccomend you use the 'go' tool to install Thrift for go. -$ go get git.apache.org/thrift.git/lib/go/thrift + $ go get git.apache.org/thrift.git/lib/go/thrift Will install the last stable release. + + +A note about optional fields +============================ + +The thrift-to-Go compiler tries to represent thrift IDL structs as Go structs. +We must be able to distinguish between optional fields that are set to their +default value and optional values which are actually unset, so the generated +code represents optional fields via pointers. + +This is generally intuitive and works well much of the time, but Go does not +have a syntax for creating a pointer to a constant in a single expression. That +is, given a struct like + + struct SomeIDLType { + OptionalField *int32 + } + +, the following will not compile: + + x := &SomeIDLType{ + OptionalField: &(3), + } + +(Nor is there any other syntax that's built in to the language) + +As such, we provide some helpers that do just this under lib/go/thrift/. E.g., + + x := &SomeIDLType{ + OptionalField: thrift.Int32Ptr(3), + } + +And so on. The code generator also creates analogous helpers for user-defined +typedefs and enums. diff --git a/lib/go/thrift/pointerize.go b/lib/go/thrift/pointerize.go new file mode 100644 index 000000000..c2ae26102 --- /dev/null +++ b/lib/go/thrift/pointerize.go @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package thrift + +/////////////////////////////////////////////////////////////////////////////// +// This file is home to helpers that convert from various base types to +// respective pointer types. This is necessary because Go does not permit +// references to constants, nor can a pointer type to base type be allocated +// and initialized in a single expression. +// +// E.g., this is not allowed: +// +// var ip *int = &5 +// +// But this *is* allowed: +// +// func IntPtr(i int) *int { return &i } +// var ip *int = IntPtr(5) +// +// Since pointers to base types are commonplace as [optional] fields in +// exported thrift structs, we factor such helpers here. +/////////////////////////////////////////////////////////////////////////////// + +func Float32Ptr(v float32) *float32 { return &v } +func Float64Ptr(v float64) *float64 { return &v } +func IntPtr(v int) *int { return &v } +func Int32Ptr(v int32) *int32 { return &v } +func Int64Ptr(v int64) *int64 { return &v } +func StringPtr(v string) *string { return &v } +func Uint32Ptr(v uint32) *uint32 { return &v } +func Uint64Ptr(v uint64) *uint64 { return &v } |