Comments (4)
Thanks for the feedback. A lot of thought was given in to how to handle the generated models, although that doesn't mean I got it right. I agree that the verbosity of creating a new struct with hard coded values is not great.
Let me walk through the thinking behind the current approach. The service is written in c#, so we need to be able to represent all the data in Go that can be represented in c#. Since structs in Go can not be null, we are forced to use a pointer for struct fields, so that the nil pointer can represent that null state.
string
is a similar case. In c# the empty string and a null string have two different meanings. For example, if I make a patch
request with a description field set to the empty string, this will update the resource's description to an empty string, while making that same patch request with the description field set to null (or not supplied), will leave the description unchanged. In Go the only way to represent a null string, is to have a nil string pointer. Therefore it was necessary to make string fields pointers also, so we can cover that null case.
Simple types like int
and bool
are handled similarly in both languages, and therefore we could get away with making these non pointer fields in Go. What you will find throughout our c# models is that for many of the cases the nullable types int?
and bool?
are used rather than simply int
or bool
. Patch requests are probably the biggest reason for this. For int?
and bool?
we will still have to use pointers to be able to represent the null state. For our c# controller methods, all new parameters that are added have to be optional for compatibility reasons, so simple types that are query parameters are usually nullable also. This means that our parameter structs in Go will also require the pointers for most cases of the simple types.
This leaves us with the decision on how to handle the simple types that are not nullable:
Option 1: Keep all simple type fields as pointers. This has the drawback of extra verbosity. The benefits are in the consistency. The consumer does not have to be confused at why some int
fields are pointers and others are not. They know ahead of time that the field is a pointer because every field is a pointer.
Option 2: Do not use pointers for simple types that are not nullable. This removes a small amount of the verbosity, making it very easy to set values on those fields. The drawback is that this only helps in a subset of fields, and introduces the inconsistency mentioned above.
What are your thoughts? Of the two options, which would have been better for your project?
from azure-devops-go-api.
@nmiodice , have you had a chance to review my response above? Do you have any feedback regarding the response?
from azure-devops-go-api.
I'm glad to see a complete Azure Devops library for go coming to fruition, thanks for your effort!
I've been following the current go-github approaches, which is to use pointers for all struct members, along with generated accessors and helper functions like https://github.com/google/go-github/blob/master/github/github.go#L1042-L1056. They discussed this topic in several issues over the years. There are some interesting comments about taking a different approach if they started over, like google/go-github#537 (comment), but they stuck with pointers for consistency.
From a consumer and new amateur Go user standpoint, I don't mind the extra verbosity too much. I appreciate the readability and don't mind checking for nil all over the place.
from azure-devops-go-api.
Thanks for the info/feedback @mcdafydd. Based on the information you provided, I think it makes the most sense to keep things as they are. I am closing this issue for now, and we can reopen if a compelling alternative pops up.
from azure-devops-go-api.
Related Issues (20)
- This repo is missing important files
- v6 + v7 pipelines module don't return continuationToken
- Get user status (active/inactive) - inactive field is deprecated.
- Support for `tokens/pats` API methods HOT 2
- v7 create service endpoint missing project name field in argument.
- Alternative of PAT for authentication? HOT 1
- Unable to create Pipeline: Value cannot be null. Parameter name: Path HOT 3
- [Feature Request] Add support to list and manage service principals identities
- Add pipeline configuration details in pipeline client
- [Feature Request] Include pipeline data in Approval object in v7 pipeline approvals
- [Feature Request] Include Security Role Assignment HOT 3
- TFVC ChangeSet changes api doesn't support pagination in the request Input. HOT 1
- Graph::ListUsers API Fails to Return `IsDeletedInOrigin` Property Value
- Question: Is it possible to retrieve a list of all repositorys and their clone urls of a project? HOT 1
- How to customize the http.client? HOT 1
- [V7] ReadIdentities doens't work as intended
- [V7] GetSelf panic
- Resource interface{} structure
- GetPolicyConfigurations returns a continuationToken but it is not taking the token back in the request
- API resource area Id <area-id> is not registered on https://dev.azure.com/<organization>
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-devops-go-api.