Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alias_generator #622

Merged
merged 10 commits into from Jul 2, 2019
Merged

Add alias_generator #622

merged 10 commits into from Jul 2, 2019

Conversation

Bobronium
Copy link
Contributor

@Bobronium Bobronium commented Jun 26, 2019

Change Summary

Add Config.alias_generator that provides ability to convert name to alias for each field of model. See docs example:

from pydantic import BaseModel

def to_camel(string: str) -> str:
     return ''.join(word.capitalize() for word in string.split('_'))
 
class Voice(BaseModel):
    name: str
    gender: str
    language_code: str
    
    class Config:
         alias_generator = to_camel

 voice = Voice(Name='Filiz', Gender='Female', LanguageCode='tr-TR')
 print(voice.language_code)
 print(voice.dict(by_alias=True))

  """
 tr-TR
 {'Name': 'Filiz', 'Gender': 'Female', 'LanguageCode': 'tr-TR'}
 """

Related issue number

None

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated

@codecov
Copy link

@codecov codecov bot commented Jun 26, 2019

Codecov Report

Merging #622 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #622   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          15     15           
  Lines        2628   2634    +6     
  Branches      516    518    +2     
=====================================
+ Hits         2628   2634    +6

@Bobronium Bobronium changed the title Added alias_provider Add alias_provider Jun 27, 2019
Copy link
Owner

@samuelcolvin samuelcolvin left a comment

Otherwise mostly looks good to me.

How do we cope with #624 which I hope to implement soon? Three different functions or the method could alternatively return a 3-tuple? Either of these solutions could be implemented later and would be backwards compatible.

docs/index.rst Outdated Show resolved Hide resolved
HISTORY.rst Outdated Show resolved Hide resolved
docs/examples/alias_provider_config.py Outdated Show resolved Hide resolved
docs/examples/alias_provider_config.py Outdated Show resolved Hide resolved
docs/examples/alias_provider_config.py Outdated Show resolved Hide resolved
tests/test_main.py Show resolved Hide resolved
tests/test_main.py Outdated Show resolved Hide resolved
Move docs to config section, reformat example
Separate test_alias_generator
@samuelcolvin samuelcolvin changed the title Add alias_provider Add alias_generator Jun 28, 2019
Copy link
Owner

@samuelcolvin samuelcolvin left a comment

otherwise LGTM.

docs/index.rst Outdated Show resolved Hide resolved
docs/index.rst Outdated Show resolved Hide resolved
docs/index.rst Outdated Show resolved Hide resolved
Bobronium and others added 4 commits Jun 28, 2019
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Copy link
Owner

@samuelcolvin samuelcolvin left a comment

sorry, noticed something else.

pydantic/main.py Outdated Show resolved Hide resolved
Bobronium and others added 3 commits Jul 2, 2019
…fig.alias_generator


Add one more condition for applying alias_generator

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@samuelcolvin samuelcolvin merged commit 010ba38 into samuelcolvin:master Jul 2, 2019
8 checks passed
@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Jul 2, 2019

great, thank you very much.

@Bobronium Bobronium deleted the alias-provider branch Jul 2, 2019
@Bobronium Bobronium restored the alias-provider branch Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants