Golang JSON Unmarshal到字段,但不是编组到JSON响应中(Golang JSON Unmarshal to field, but NOT marshal into JSON response)
我希望能够访问由JSON unmarshal产生的结构字段,但是我想使用相同的结构来在封送时隐藏字段。
例:
type MyStruct struct { GoodField string `json:"goodField"` SecretField string `json:"secret"` }
传入的JSON得到解组并且
secret
字段可访问在服务器响应中使用相同的MyStruct
,但隐藏secret
字段。我看过使用
omitempty
和-
标签,而不是工作。I want to be able to access a struct field that's resulted from a JSON unmarshal, BUT I want to use that same struct to hide the field when it is marshaled.
Example:
type MyStruct struct { GoodField string `json:"goodField"` SecretField string `json:"secret"` }
Incoming JSON gets unmarshaled and the
secret
field is accessible Use the sameMyStruct
in the server response but hide thesecret
field.I've looked at using
omitempty
and-
tags, not working.
原文:https://stackoverflow.com/questions/47853120
最满意答案
是的,我是对的
在web上我很久没有更新管理模式,这就是我遇到这个问题的原因。
解决方案:将 admin中的
schema.rb
复制到web repo中,因为两个项目使用相同的数据库。愿这个答案对别人也有帮助。
Yes, I was right
It has been long time in web that I haven’t updated schema from admin that’s why I’ve faced this issue.
Solution: Copied
schema.rb
from admin into web repo, because both projects use same DB.May the answer will helpful for someone also.
相关问答
更多-
错误无法在本地Windows 7 Ruby on Rails上加载'pg'gem(Error cannot load 'pg' gem on local windows 7 Ruby on Rails)[2023-01-25]
你的宝石清单显示: pg(0.15.1 x64-mingw32) 但你的Gemfile.lock显示 平台x86-mingw32 我认为你的问题涉及混合平台(32位(x86)与64位(x64)) 试着调查一下。 Your gem list shows: pg (0.15.1 x64-mingw32) But your Gemfile.lock shows PLATFORMS x86-mingw32 I think your issue is related to a mixed platform (32bi ... -
是的,我是对的 在web上我很久没有更新管理模式,这就是我遇到这个问题的原因。 解决方案:将 admin中的schema.rb复制到web repo中,因为两个项目使用相同的数据库。 愿这个答案对别人也有帮助。 Yes, I was right It has been long time in web that I haven’t updated schema from admin that’s why I’ve faced this issue. Solution: Copied schema.rb fr ...
-
我用自制软件安装pg,解决了我的问题: $ brew install postgres I used homebrew to install pg and that solved my problems: $ brew install postgres
-
在深入研究这个问题之后,我对@MartinZinovsky在评论中提到的一些事情表示怀疑,这不是pg gem的问题,而是另一个没有更新的gem,特别是ActiveRecord。 这是错误的一部分,让我想到它出现在两个错误中: ensure its version is at the minimum required by ActiveRecord 所以,不要像马丁建议的那样进行bundle update pg ,而是通过bundle update pg来完成并更新所有可用更新的gem。 当这样做的时候,它确 ...
-
因此,看起来好像url_name存在于模式中,但另一个人删除了迁移文件。 为了解决我运行删除列迁移,然后添加列迁移,它解决了问题。 So it looks as though url_name was present in the schema but another person removed the migration file. To resolve I ran a remove column migration then did an add column migration and it res ...
-
您已将hstore扩展安装在名为hstore的模式中,该模式大概不在您的默认search_path 。 您必须执行以下其中一项操作: 在连接建立期间将hstore添加到search_path ; 使用ALTER USER ... SET或ALTER DATABASE ... SET添加hstore到search_path ; 将hstore扩展名从hstore模式移入public ; 要么 模式 - 限定所有对hstore引用,例如hstore.hstore(...) 。 这也必须为运营商完成; ->变成O ...
-
您的数据库架构是在Mysql数据库上生成的,并包含Mysql特定的选项。 在您的情况下,它是ENGINE选项。 我不确定它是自动生成还是您手动将这些选项添加到迁移中。 尝试运行迁移而不是加载架构: heroku run rake db:migrate 你也可以在Heroku上使用Mysql。 你需要添加适当的插件。 Your database schema was generated on Mysql database and contains Mysql specific options. In you ...
-
以下是我解决问题的方法,首先更新自制软件: brew update 如果您收到此错误: error: Your local changes to the following files would be overwritten by merge: 然后: cd /usr/local/cellar git reset --hard FETCH_HEAD 完成自制程序更新后: sudo brew install postgresql 哪个将安装9.1.3(或最新版本的pg)和任何依赖项。 Here's how ...
-
PG :: UndefinedTable ERROR ruby on rails heroku(PG::UndefinedTable ERROR ruby on rails heroku)[2023-09-26]
看到你的rake db:migrate:status你有一个问题就是迁移Add code to events应该是在Create events迁移之后,因为你在event中进行了更改,但之后正在创建表。 所以你应该解决这个问题,对于临时修复你可以做的是: rake db:migrate:up VERSION=20161219214142 20161219214142是您Create Events迁移的版本,因此这将为您运行特定的迁移并创建events表,然后如果您将运行rake db:migrate那么它 ... -
我会删除heroku上的数据库并重新创建它(你将丢失保存到数据库的所有数据): heroku pg:reset DATABASE_URL --confirm your_app_name 将your_app_name替换为heroku上的应用程序名称,但保留DATABASE_URL。 然后运行: heroku run rake db:setup 它将运行以下两个命令: heroku run rake db:migrate heroku run rake db:seed ...