Giter Site home page Giter Site logo

Comments (4)

tedchamb avatar tedchamb commented on September 24, 2024

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.

tedchamb avatar tedchamb commented on September 24, 2024

@nmiodice , have you had a chance to review my response above? Do you have any feedback regarding the response?

from azure-devops-go-api.

mcdafydd avatar mcdafydd commented on September 24, 2024

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.

tedchamb avatar tedchamb commented on September 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.